import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[][] model = new int[3][3]; int[][] box = new int[3][3]; model[0][0] = 8; model[0][1] = 1; model[0][2] = 6; model[1][0] = 3; model[1][1] = 5; model[1][2] = 7; model[2][0] = 4; model[2][1] = 9; model[2][2] = 2; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { box[i][j] = scan.nextInt(); } } int[][] a = mirrorY(model); int[][] b = mirrorX(model); int[][] c = diag1(model); int[][] d = diag2(model); int[][] e = mirrorX(d); int[][] f = mirrorX(c); int[][] g = mirrorY(b); List vars = Arrays.asList(model, a, b, c, d, e, f, g); int minChange = Integer.MAX_VALUE; for (int i = 0; i < 8; i++) minChange = Math.min(minChange, change(vars.get(i), box)); System.out.println(minChange); } private static int change(int[][] model, int[][] input) { int n = model.length; int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans += Math.abs(model[i][j] - input[i][j]); } } return ans; } private static int[][] mirrorY(int[][] model) { int n = model.length; int[][] ans = new int[n][n]; for (int i = 0; i < n; i++) { ans[i] = Arrays.copyOf(model[i], n); for (int j = 0; j < n / 2; j++) { ans[i][j] = model[i][n - j - 1]; ans[i][n - j - 1] = model[i][j]; } } return ans; } private static int[][] mirrorX(int[][] model) { int n = model.length; int[][] ans = new int[n][n]; for (int i = 0; i < n / 2; i++) { for (int j = 0; j < n; j++) { ans[i][j] = model[n - i - 1][j]; ans[n - i - 1][j] = model[i][j]; } } if (n % 2 == 1) for (int i = 0; i < n; i++) ans[n / 2][i] = model[n / 2][i]; return ans; } private static int[][] diag1(int[][] model) { int n = model.length; int[][] ans = new int[n][n]; for (int i = 0; i < n; i++) { ans[i][i] = model[i][i]; for (int j = i + 1; j < n; j++) { ans[i][j] = model[j][i]; ans[j][i] = model[i][j]; } } return ans; } private static int[][] diag2(int[][] model) { int n = model.length; int[][] ans = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans[i][j] = model[n - j - 1][n - i - 1]; ans[n - j - 1][n - i - 1] = model[i][j]; } } return ans; } }