def solve(B0, C0, B1, C1): C0 %= B0 C1 %= B1 for n in range(B0 * B1): if n % B0 == C0 and n % B1 == C1: return n return "NaN" B0, C0 = map(int, input().split()) B1, C1 = map(int, input().split()) print(solve(B0, C0, B1, C1))