A,B,N,M = map(int, input().split()) while 1: if A < B: B = B - M A += 1 if A > B: B = B + M A -= 1 break elif A > B: A = A - N B += 1 if A < B: A = A + N B -= 1 break print(min(A, B))