A,B,a,b=map(int,input().split()) n=a while True: if n%B==b: print(n) exit() else: n+=A