main :: IO () main = do [aw,ab] <- map read . words <$> getLine [bw,bb] <- map read . words <$> getLine [c,d] <- map read . words <$> getLine print $ solve aw ab bw bb c d solve :: Int -> Int -> Int -> Int -> Int -> Int -> Int solve aw ab bw bb 0 0 = aw solve aw ab bw bb 0 d = if bw >= d then solve (aw+d) ab (bw-d) bb 0 0 else solve (aw+bw) ab 0 bb 0 0 solve aw ab bw bb c d = if ab >= c then solve aw (ab-c) bw (bb+c) 0 d else solve (aw-(c-ab)) 0 (bw+(c-ab)) (bb+ab) 0 d