(a,b),(x,y),(c,d)=$<.map{|e|e.split.map(&:to_i)} z=[c-b,0].max x+=z a-=z a+=[x,d].min p a