b0, c0 = map(int, input().split()) b1, c1 = map(int, input().split()) for i in range(b0*b1+1): if (i-c0) % b0 == 0 and (i-c1) % b1 == 0: print(i) break if b0 * b1 == i: print("NaN")