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