import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int cost = 0; int temp_cost = -1; int[] square_1 = {8,1,6,3,5,7,4,9,2}; int[] square_2 = {4,3,8,9,5,1,2,7,6}; int[] square_3 = {2,9,4,7,5,3,6,1,8}; int[] square_4 = {6,7,2,1,5,9,8,3,4}; int[] square_5 = {6,1,8,7,5,3,2,9,4}; int[] square_6 = {8,3,4,1,5,9,6,7,2}; int[] square_7 = {4,9,2,3,5,7,8,1,6}; int[] square_8 = {2,7,6,9,5,1,4,3,8}; int[][] array_squares ={square_1,square_2,square_3,square_4,square_5,square_6,square_7,square_8}; int[] square = new int[9]; for(int a = 0 ; a < 9 ; a++) square[a] = in.nextInt(); for(int x = 0 ; x < 8 ; x++){ for(int y = 0 ; y < 9 ; y++) { if(square[y]!=array_squares[x][y]) cost+=Math.abs(square[y]-array_squares[x][y]); } if(temp_cost == -1) temp_cost = cost; else if(cost < temp_cost) temp_cost = cost; cost = 0; } cost = temp_cost; System.out.println(cost); } }