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