def INT(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) A, B, a, b = MI() for i in range(10**7): if (i * A + a) % B == b: print(i * A + a) exit()