The passage from the first line to the second is not correct; the set of a's such that gcd(a,m) = 1 may not be disjoint from the gcd(a,n) = 1.
One suggestion: instead of working with the sums, try using Euler's formula for \varphi:
\varphi\left(n\right) = n\left(\ 1 - \frac{1}{p_{1}}\right)\left(\ 1 - \frac{1}{p_{2}}\right)...\left(\ 1 - \frac{1}{p_{k}}\right)
Where p_{1},...,p_{n} are the prime factors of n.