a,b = map(int,input().split()) c,d = map(int,input().split()) s = b % a t = d % c check = 0 for i in range(1000): for j in range(1000): if s + a*i == t + c*j: print(s+a*i) check = 1 exit() if s + a*i < t + c*j: break print('NaN')