a,b,n,m = map(int, input().split()) def is_ok(k): x = max(0, (a-k)//n) if a-n*x >= k and b+x >= k: return True y = max(0, (b-k)//m) if a+y >= k and b-m*y >= k: return True return False ok = 0 ng = 10**18+1 while ok+1 < ng: c = (ok+ng)//2 if is_ok(c): ok = c else: ng = c print(ok)