import sys, itertools sys.setrecursionlimit(500005) stdin = sys.stdin ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().strip() a, b, n, m = na() ans = 0 def con(a, b, n): if (a - b) % n == 0: return (a - b) // n - 1 else: return (a - b) // n if a < b: c = con(b, a, m) a += c b -= m * (c + 1) elif a > b: c = con(a, b, n) a -= n * (c + 1) b += c print(max(a, b))