p, q, a, b = map(int, input().split()) v = a for i in range(100000): if v % q == b: print(v) exit() v += p