ma, mb, ra, rb = map(int, input().split()) ans = 10**18 for x in range(10001): res = ma * x + ra if res % mb == rb: ans = min(ans, res) res = mb * x + rb if res % ma == ra: ans = min(ans, res) print(ans)