a,b,n,m = map(int,input().split()) if a < b: a,b = b,a n,m = m,n k = (a-b)//(n+1) a -= k*n b += k print(max(min(a,b), min(a-n,b+1)))