A,B,a,b = map(int,input().split()) if (a == A and b == B) or (a==0 and b==0): print(0) else : for x in range(1, 2**10000): aa = x%A bb = x%B if aa == a and bb == b: print(x) break