object Solution {

  def listdiff(a: List[Int], b: List[Int]) =
    (a zip b).map{case (x: Int, y: Int) => math.abs(x - y)}.sum

  def main(args: Array[String]) {
    val squares = List(
      "2 9 4 7 5 3 6 1 8",
      "6 7 2 1 5 9 8 3 4",
      "8 1 6 3 5 7 4 9 2",
      "4 3 8 9 5 1 2 7 6",
      "4 9 2 3 5 7 8 1 6",
      "8 3 4 1 5 9 6 7 2",
      "6 1 8 7 5 3 2 9 4",
      "2 7 6 9 5 1 4 3 8"
    ).map(_.split(" ").map(_.toInt).toList)
    val sc = new java.util.Scanner(System.in)
    val in = for (i <- (1 to 9).toList) yield sc.nextInt
    val diffs = for (square <- squares) yield listdiff(square, in)
    println(diffs.min)
  }
}