import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int[][] mat = new int[3][3]; static int[][] ms1 = {{8,1,6},{3,5,7},{4,9,2}}; static int[][] ms2 = {{6,1,8},{7,5,3},{2,9,4}}; static int[][] ms3 = {{4,9,2},{3,5,7},{8,1,6}}; static int[][] ms4 = {{2,9,4},{7,5,3},{6,1,8}}; static int[][] ms5 = {{8,3,4},{1,5,9},{6,7,2}}; static int[][] ms6 = {{4,3,8},{9,5,1},{2,7,6}}; static int[][] ms7 = {{6,7,2},{1,5,9},{8,3,4}}; static int[][] ms8 = {{2,7,6},{9,5,1},{4,3,8}}; static int thecost(int[][] current,int[][] ref){ int c=0; for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ c += Math.abs(current[i][j]-ref[i][j]); } } return c; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ mat[i][j]=scan.nextInt(); } if (i<2) scan.nextLine(); } int minCost = 2147483647; int[] costs = new int[8]; costs[0]=thecost(mat,ms1); costs[1]=thecost(mat,ms2); costs[2]=thecost(mat,ms3); costs[3]=thecost(mat,ms4); costs[4]=thecost(mat,ms5); costs[5]=thecost(mat,ms6); costs[6]=thecost(mat,ms7); costs[7]=thecost(mat,ms8); for (int i=0; i<8; i++) if (costs[i]