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