import sys readline=sys.stdin.readline A,B,a,b=map(int,readline().split()) ans=0 while ans%A!=a or ans%B!=b: ans+=1 print(ans)