Let g=gcd(a,m), so we have a=g⋅k,m=g⋅l,gcd(l,k)=1,first observation is that if we want gcd(a,m)=gcd(a+x,m), x has to be a multiple of g, let x=n⋅g. Furthermore, k+n and l have to be coprime, so we need to find how many numbers ranging from k to k+l are coprime with l. For numbers bigger than l, if gcd(k+x,l)=1, then gcd((k+x)modl,l)=1. Since (k+x)modl<l, what we actually need to find is the number of numbers that are coprime with l and smaller than l, i.e. φ(l).