a,b,c,d=map(int,input().split()) for i in range(1000000): count=a*i+c if count%b==d: print(count) break