A,B,a,b = list(map(int,input().split())) x = 0 while(True): if((A*x+a)%B == b): print(A*x+a) exit() else: x += 1