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