import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { int[][] a = new int[3][3]; Scanner in = new Scanner(System.in); a[0][0] = in.nextInt(); a[0][1] = in.nextInt(); a[0][2] = in.nextInt(); in.nextLine(); a[1][0] = in.nextInt(); a[1][1] = in.nextInt(); a[1][2] = in.nextInt(); in.nextLine(); a[2][0] = in.nextInt(); a[2][1] = in.nextInt(); a[2][2] = in.nextInt(); int[] cost = new int[8]; int[][] ini = {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}; cost[0] = findDiff(a, ini); int[][] out = new int[3][3]; rotate(ini, out, 3); cost[1] = findDiff(a, out); int[][] out2 = new int[3][3]; rotate(out, out2, 3); cost[2] = findDiff(a, out2); int[][] out3 = new int[3][3]; rotate(out2, out3, 3); cost[3] = findDiff(a, out3); int[][] out4 = new int[3][3]; columnSwitch(ini, out4, 3); cost[4] = findDiff(a, out4); int[][] out5 = new int[3][3]; columnSwitch(out, out5, 3); cost[5] = findDiff(a, out5); int[][] out6 = new int[3][3]; columnSwitch(out2, out6, 3); cost[6] = findDiff(a, out6); int[][] out7 = new int[3][3]; columnSwitch(out3, out7, 3); cost[7] = findDiff(a, out7); int minCost = cost[0]; for (int i = 0; i < 8; i++) { if (cost[i] < minCost) minCost = cost[i]; } System.out.println(minCost); } public static int findDiff(int[][] a, int[][] b) { int cost = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cost += Math.abs(a[i][j] - b[i][j]); } } return cost; } public static int[][] rotate(int[][] a, int[][] out, int n) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) out[j][n - 1 - i] = a[i][j]; return out; } public static int[][] columnSwitch(int[][] a, int[][] out, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { out[i][j] = a[i][j]; } } for (int i = 0; i < n; i++) { int tmp = out[i][0]; out[i][0] = out[i][n - 1]; out[i][n - 1] = tmp; } return out; } }