A,B,a,b = map(int,input().split()) i = 0 while True: if i%A == a and i%B==b: print(i) exit() i += 1