b0, c0 = map(int, input().split())
b1, c1 = map(int, input().split())
for i in range(10**5):
    if i%b0 == c0%b0 and i%b1 == c1%b1: print(i); break
else: print("NaN")