def ris() gets.split.map &:to_i end def ri() gets.to_i end MOD = 1_000_000_007 # 998_244_353 aw,ab=ris bw,bb=ris c,d=ris mn=[c,ab].min c,ab,bb=c-mn,ab-mn,bb+mn aw,bw=aw-c,bw+c p [d,bw].min+aw