import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[][] m = new int[3][]; for (int i = 0; i < m.length; i++) { m[i] = new int[3]; for (int j = 0; j < m[i].length; j++) { m[i][j] = in.nextInt(); } } int[][] magic = {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}; int min = Integer.MAX_VALUE; min = Integer.min(min, cost(m, magic)); min = Integer.min(min, cost(m, magic = rotate(magic))); min = Integer.min(min, cost(m, magic = rotate(magic))); min = Integer.min(min, cost(m, magic = rotate(magic))); min = Integer.min(min, cost(m, magic = flip(rotate(magic)))); min = Integer.min(min, cost(m, magic = rotate(magic))); min = Integer.min(min, cost(m, magic = rotate(magic))); min = Integer.min(min, cost(m, magic = rotate(magic))); System.out.println(min); } private static int[][] rotate(int[][] mat) { int[][] ret = new int[mat.length][mat.length]; for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat.length; j++) { ret[j][mat.length - 1 - i] = mat[i][j]; } } return ret; } private static int[][] flip(int[][] mat) { int[][] ret = new int[mat.length][mat.length]; for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat.length; j++) { ret[mat.length - 1 - i][j] = mat[i][j]; } } return ret; } private static int cost(int[][] m, int[][] magic) { int cost = 0; for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[i].length; j++) { cost += Math.abs(m[i][j] - magic[i][j]); } } return cost; } }