import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = 3; int[][] m = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { m[i][j] = in.nextInt(); } } int[][] m1 = { {8,1,6}, {3,5,7}, {4,9,2} }; int[][] m2 = { {6,1,8}, {7,5,3}, {2,9,4} }; int[][] m3 = { {8,3,4}, {1,5,9}, {6,7,2} }; int[][] m4 = { {4,3,8}, {9,5,1}, {2,7,6} }; int[][] m5 = { {6,7,2}, {1,5,9}, {8,3,4} }; int[][] m6 = { {2,7,6}, {9,5,1}, {4,3,8} }; int[][] m7 = { {4,9,2}, {3,5,7}, {8,1,6} }; int[][] m8 = { {2,9,4}, {7,5,3}, {6,1,8} }; int countSquares = 8; int[][][] magicSquares = new int[countSquares][n][n]; magicSquares[0] = m1; magicSquares[1] = m2; magicSquares[2] = m3; magicSquares[3] = m4; magicSquares[4] = m5; magicSquares[5] = m6; magicSquares[6] = m7; magicSquares[7] = m8; int minCost = 999999; for (int i = 0; i < countSquares; i++) { int cost = 0; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (m[j][k] != magicSquares[i][j][k]) cost += Math.abs(m[j][k] - magicSquares[i][j][k]); } } if (cost < minCost) minCost = cost; } System.out.println(minCost); } }