A,B,a,b = map(int,input().split()) n = 0 while True: if (A*n+a)%B == b: print(A*n+a) break