class Calc0149 def initialize(args) args = args.map { |l| l.chomp.split(/\s+/) } @aw, @ab = args.shift.map(&:to_i) @bw, @bb = args.shift.map(&:to_i) @c, @d = args.shift.map(&:to_i) end def run w1 = [0, @c - @ab].max w2 = [@d, @bw + w1].min @aw - w1 + w2 end end puts Calc0149.new(STDIN.readlines).run if __FILE__ == $0