import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String[] line1Arr = scan.nextLine().split(" "); String[] line2Arr = scan.nextLine().split(" "); String[] line3Arr = scan.nextLine().split(" "); int[] line1 = new int[3]; int[] line2 = new int[3]; int[] line3 = new int[3]; for(int i = 0; i < 3; i++) { line1[i] = Integer.parseInt(line1Arr[i]); line2[i] = Integer.parseInt(line2Arr[i]); line3[i] = Integer.parseInt(line3Arr[i]); } int[][] magicArr = {line1, line2, line3}; List costArr = new ArrayList<>(8); int[][] arr1 = { {2, 7, 6}, {9, 5, 1}, {4, 3, 8} }; int[][] arr2 = { {4, 3, 8}, {9, 5, 1}, {2, 7, 6} }; int[][] arr3 = { {8, 1, 6}, {3, 5, 7}, {4, 9, 2} }; int[][] arr4 = { {6, 1, 8}, {7, 5, 3}, {2, 9, 4} }; int[][] arr5 = { {6, 7, 2}, {1, 5, 9}, {8, 3, 4} }; int[][] arr6 = { {8, 3, 4}, {1, 5, 9}, {6, 7, 2} }; int[][] arr7 = { {2, 9, 4}, {7, 5, 3}, {6, 1, 8} }; int[][] arr8 = { {4, 9, 2}, {3, 5, 7}, {8, 1, 6} }; int[][][] templateArr = {arr1, arr2, arr3, arr4, arr5, arr6, arr7, arr8}; for(int i = 0; i < templateArr.length; i++) { int cost = 0; for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { cost += Math.abs(templateArr[i][j][k] - magicArr[j][k]); } } costArr.add(cost); } Collections.sort(costArr); System.out.println(costArr.get(0)); } }