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() Flag = True ans = 9999999999999999999 while Flag: if a < b: ans = a a += 1 b -= m if not(a < b): Flag = False elif a > b: ans = b a -= n b += 1 if not(a > b): Flag = False else: Flag = False print(max(min(a, b), ans))