main = getContents >>= print . solve . map (map read . words) . lines solve :: [[Integer]] -> Integer solve [[aw, ab], [bw, bb], [c, d]] | ab < c = if d - c + ab <= bw then aw + ab + d - c else aw + bw | otherwise = if bw <= d then aw + bw else aw + d