A,B,a,b=map(int, input().split()) #A=list(map(int, input().split())) for i in range(10**7+100): if i%A==a and i%B==b: print(i) exit()