import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Sol { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int[][] a = new int[3][3]; String[] s; int i, j, k; for (i = 0; i < 3; i++) { s = reader.readLine().split(" "); for (j = 0; j < 3; j++) { a[i][j] = Integer.parseInt(s[j]); } } int[][][] m = { {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}, {{6, 1, 8}, {7, 5, 3}, {2, 9, 4}}, {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}}, {{6, 7, 2}, {1, 5, 9}, {8, 3, 4}}, {{4, 3, 8}, {9, 5, 1}, {2, 7, 6}}, {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}}, {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}, {{2, 9, 4}, {7, 5, 3}, {6, 1, 8}} }; int cost, min = Integer.MAX_VALUE; for (i = 0; i < 8; i++) { cost = 0; for (j = 0; j < 3; j++) { for (k = 0; k < 3; k++) { cost += Math.abs(m[i][j][k] - a[j][k]); } } if (cost < min) { min = cost; } } System.out.println(min); reader.close(); } }