import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* * Enter your code here. Read input from STDIN. Print output to STDOUT. Your * class should be named Solution. */ Scanner in = new Scanner(System.in); String row1 = in.nextLine(); String row2 = in.nextLine(); String row3 = in.nextLine(); int[][] m1 = new int[][]{{4,9,2}, {3,5,7}, {8,1,6}}; int[][] m2 = new int[][]{{2,9,4}, {7,5,3}, {6,1,8}}; int[][] m3 = new int[][]{{4,3,8}, {9,5,1}, {2,7,6}}; int[][] m4 = new int[][]{{2,7,6}, {9,5,1}, {4,3,8}}; int[][] m5 = new int[][]{{8,1,6}, {3,5,7}, {4,9,2}}; int[][] m6 = new int[][]{{6,1,8}, {7,5,3}, {2,9,4}}; int[][] m7 = new int[][]{{8,3,4}, {1,5,9}, {6,7,2}}; int[][] m8 = new int[][]{{6,7,2}, {1,5,9}, {8,3,4}}; int[][][] listOfMatrix = new int[][][]{m1, m2, m3, m4, m5, m6, m7, m8}; // Given matrix int[][] m = new int[3][3]; String[] row1arr = row1.split(" "); String[] row2arr = row2.split(" "); String[] row3arr = row3.split(" "); for ( int i = 0; i < row1arr.length; i ++ ) { m[0][i] = Integer.parseInt(row1arr[i]); } for ( int i = 0; i < row2arr.length; i ++ ) { m[1][i] = Integer.parseInt(row2arr[i]); } for ( int i = 0; i < row3arr.length; i ++ ) { m[2][i] = Integer.parseInt(row3arr[i]); } int min = Integer.MAX_VALUE; for ( int[][] i : listOfMatrix) { int diff = findDiff(i, m); if ( diff < min) min = diff; } System.out.println(min); } private static int findDiff(int[][] m1, int[][] m2) { int diff = 0; for ( int i = 0; i < 3; i ++ ) { for ( int j = 0; j < 3; j++ ) { diff += Math.abs(m1[i][j]-m2[i][j]); } } return diff; } }