import System.IO import Data.Matrix as M -- all 3x3 magic squares magicSquares = map M.fromLists [ [[4, 9, 2] ,[3, 5, 7] ,[8, 1, 6]] , [[8, 3, 4] ,[1, 5, 9] ,[6, 7, 2]] , [[8, 1, 6] ,[3, 5, 7] ,[4, 9, 2]] , [[4, 3, 8] ,[9, 5, 1] ,[2, 7, 6]] , [[6, 1, 8] ,[7, 5, 3] ,[2, 9, 4]] , [[2, 7, 6] ,[9, 5, 1] ,[4, 3, 8]] , [[2, 9, 4] ,[7, 5, 3] ,[6, 1, 8]] , [[6, 7, 2] ,[1, 5, 9] ,[8, 3, 4]]] matrixChangeCost :: Matrix Integer -> Matrix Integer -> Integer matrixChangeCost m0 m1 = sum ( M.toList ( M.elementwiseUnsafe absDiff m0 m1 )) where absDiff n0 n1 = abs (n0 - n1) main = do l0 <- getLine l1 <- getLine l2 <- getLine let m = (map (take 3 . map read . words) [l0, l1, l2]) :: [[Integer]] putStrLn $ show $ minimum $ map (matrixChangeCost (M.fromLists m)) magicSquares