A, B, a, b = map(int, input().split()) for M in range(0, 10**7): if (A * M + a) % B == b: print(A * M + a) exit()