X,Y,a,b=map(int,input().split()) for z in range(10**9): if z%X==a and z%Y==b: print(z) exit()