from math import * def lcm(x, y): return x*y//gcd(x, y) B0, C0 = map(int, input().split()) B1, C1 = map(int, input().split()) C0 %= B0 C1 %= B1 for i in range(lcm(B0, B1)): if i%B0==C0 and i%B1==C1: print(i) break else: print('NaN')