Modulo Property::
(A-B)%C = (A%C - B%C +C ) % C
Here what we have given;
A%M = B%M
so;
A%M - B%M = 0
let add M to both sides;
A%M - B%M + M = M
let apply modulo to both sides;
(A%M- B%M + M)% M = (M%M) = 0
so at the end we got Modulo Property::
(A-B)%M = 0
So simply do (A-B)
find All the factors of it
and the Max factor is the ans.