object Solution {
    
    val theMagicSquare = Array(Array(2,7,6),Array(9,5,1),Array(4,3,8))
    val theMagicSquares = Array(theMagicSquare, theMagicSquare.reverse, theMagicSquare.map(_.reverse), theMagicSquare.map(_.reverse).reverse, theMagicSquare.transpose, theMagicSquare.transpose.reverse, theMagicSquare.transpose.map(_.reverse), theMagicSquare.transpose.map(_.reverse).reverse)

    def main(args: Array[String]): Unit = {
//        println(theMagicSquares.map(_.map(_.mkString).mkString(sep = "\n")).mkString(sep = "\n\n"))
        val input = Array.fill(3)(readLine().split(" ").map(_.toInt))
        println(theMagicSquares.map(compSquares(input)).min)
    }
    
    def compSquares(s1: Array[Array[Int]])(s2: Array[Array[Int]]): Int = {
        s1.zip(s2).map({ case (r1,r2) => r1.zip(r2).map({ case (e1,e2) => (e1-e2).abs }).sum }).sum
    }
}