Approach hintTry it first, then revealHide
Let g = gcd(a, m). After dividing by g, the condition becomes coprimality with m / g over a complete residue system, so the answer is Euler's totient phi(m / g).
Let g = gcd(a, m). After dividing by g, the condition becomes coprimality with m / g over a complete residue system, so the answer is Euler's totient phi(m / g).