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