b0,c0=list(map(int,input())) b1,c1=list(map(int,input())) for i in range(1,b0*b1+1): if (i-c0)//b0==0 and (i-c1)//b1==0: print (i) break if i==b0*b1+1: print("NaN")