A, B, N, M = map(int, input().split())

max_t = min(A, B)

# Function to compute best T when converting hamburgers (case 2: y=0)
def compute_case2():
    best = 0
    denominator = N + 1
    if denominator == 0:
        return 0  # This case should not happen as N >=1
    x_opt = (A - B) // denominator
    candidates = [x_opt - 2, x_opt -1, x_opt, x_opt +1, x_opt +2, 0, A//N]
    seen = set()
    for x in candidates:
        x = max(0, x)
        if N == 0:
            x = 0
        else:
            x = min(x, A // N)
        if x in seen:
            continue
        seen.add(x)
        a_new = A - x * N
        if a_new < 0:
            continue
        b_new = B + x
        current = min(a_new, b_new)
        if current > best:
            best = current
    return best

current_case2 = compute_case2()
if current_case2 > max_t:
    max_t = current_case2

# Function to compute best T when converting natto (case 3: x=0)
def compute_case3():
    best = 0
    denominator = M + 1
    if denominator == 0:
        return 0  # This case should not happen as M >=1
    y_opt = (B - A) // denominator
    candidates = [y_opt - 2, y_opt -1, y_opt, y_opt +1, y_opt +2, 0, B//M]
    seen = set()
    for y in candidates:
        y = max(0, y)
        if M == 0:
            y = 0
        else:
            y = min(y, B // M)
        if y in seen:
            continue
        seen.add(y)
        b_new = B - y * M
        if b_new < 0:
            continue
        a_new = A + y
        current = min(a_new, b_new)
        if current > best:
            best = current
    return best

current_case3 = compute_case3()
if current_case3 > max_t:
    max_t = current_case3

print(max_t)