A,B,a,b=map(int,input().split())
i=0
while i%A-a|i%B-b:i+=1
print(i)