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