from itertools import count A, B, a, b = map(int, input().split()) for x in count(0): if x % A == a and x % B == b: print(x) exit()