Little Jhool and his primes

Can anyone explain the approach in detail ,i am not able to get it through editorial
This is the problem

the question comes down to-
given a,d,m find x for which (a+dx) ≡ 0(mod m)
so we can write the equation as
now this is a linear diophantine equation which can be solved easily using extended euclidean algorithm
for any doubts u can check my code-


Thanks bro for help