def Main(): a,b,p,q=map(int,input().split()) ans=p while True: if ans%b==q: print(ans) return ans+=a Main()