a, b, x, y = map(int, input().split())
p = a / (x - 1)
if p % 1 != 0:
    p = (p + 1) // 1
k = p * x - 1
g = b / (y - 1)
if g % 1 != 0:
    g = (g + 1) // 1
ans = g * y - 1
print(int(max(ans - (b % (y - 1)), k - (a % (x - 1)))))
