import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner s=new Scanner(System.in); int[][] arr=new int[3][3]; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) arr[i][j]=s.nextInt(); } int[] outr_arr={arr[0][0],arr[1][0],arr[2][0],arr[2][1],arr[2][2],arr[1][2],arr[0][2],arr[0][1]}; int[] other={4,3,8,1,6,7,2,9}; int[] other1={8,3,4,9,2,7,6,1}; int[] other2={2,7,6,1,8,3,4,9}; int[] other3={6,7,2,9,4,3,8,1}; int c=0,min=100; for(int i=0;i<4;i++) { c=Math.abs(5-arr[1][1]); for(int j=0;j<8;j++) { if(outr_arr[j]!=other[(j+(i*2))%8]) { c+=Math.abs(outr_arr[j]-other[(j+(i*2))%8]); } } if(min>c) min=c; } for(int i=0;i<4;i++) { c=Math.abs(5-arr[1][1]); for(int j=0;j<8;j++) { if(outr_arr[j]!=other1[(j+(i*2))%8]) { c+=Math.abs(outr_arr[j]-other1[(j+(i*2))%8]); } } if(min>c) min=c; } for(int i=0;i<4;i++) { c=Math.abs(5-arr[1][1]); for(int j=0;j<8;j++) { if(outr_arr[j]!=other2[(j+(i*2))%8]) { c+=Math.abs(outr_arr[j]-other2[(j+(i*2))%8]); } } if(min>c) min=c; } for(int i=0;i<4;i++) { c=Math.abs(5-arr[1][1]); for(int j=0;j<8;j++) { if(outr_arr[j]!=other3[(j+(i*2))%8]) { c+=Math.abs(outr_arr[j]-other3[(j+(i*2))%8]); } } if(min>c) min=c; } System.out.println(min); } }