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