A,B,a,b=map(int,input().split()) for i in range(B): if((a+A*i)%B==b): print(a+A*i) break