def min(a,b); a < b ? a : b; end def max(a,b); a > b ? a : b; end A,B = 2.times.map{ gets.split.map(&:to_i) } C,D = gets.split.map(&:to_i) puts max(0, C - A[1]).then{|w| A[0] - w + min(D, B[0] + w) }