A, B, N, M = gets.split.map(&:to_i) puts (0 .. 10**19).bsearch { |c| next false if [A, B].max < c x = A - c y = B - c if x < 0 if y < 0 false else -x <= y / M end else if y < 0 -y <= x / N else true end end }