A,B,a,b = map(int, input().split()) ans = float("inf") for x in range(10**7+1): if x%A==a and x%B==b: ans = min(ans, x) print(ans)