while 1: r,b,m,n=map(int,raw_input().split()) while(r%n-b)%r:r=0-r/m/n|r+m print r