B1,C1=map(int,input().split()) B2,C2=map(int,input().split()) for n in range(100000): if (n-C1)%B1==0 and (n-C2)%B2==0: exit(print(n)) print("NaN")