a, b, n, m = map(int, input().split()) if a == b: ans = a elif a > b: x = (a-b) // n b += x a -= x * n while a < b: a += n b -= 1 ans = b else: x = (b-a) // m a += x b -= x * m while b < a: b += m a -= 1 ans = a print(ans)