import Control.Arrow

main :: IO ()
main = interact $ show . (day09 &&& day09 . map reverse) . parse

parse :: String -> [[Int]]
parse = map (map read . words) . lines

day09 :: [[Int]] -> Int
day09 =
  sum .
  map (sum . map last . takeWhile (not . null) . iterate (zipWith (-) =<< tail))
