import java.io.*; import java.util.*; public class Solution { public static void main(String args[]){ Scanner input = new Scanner(System.in); int num[]=new int[9]; int ans[] = new int[8]; for(int i=0;i<9;i++) num[i]=input.nextInt(); int m1[]={8,1,6,3,5,7,4,9,2}; int m2[]={4,3,8,9,5,1,2,7,6}; int m3[]={2,9,4,7,5,3,6,1,8}; int m4[]={6,7,2,1,5,9,8,3,4}; int m5[]={6,1,8,7,5,3,2,9,4}; int m6[]={8,3,4,1,5,9,6,7,2}; int m7[]={4,9,2,3,5,7,8,1,6}; int m8[]={2,7,6,9,5,1,4,3,8}; ans[0]=check(num,m1); ans[1]=check(num,m2); ans[2]=check(num,m3); ans[3]=check(num,m4); ans[4]=check(num,m5); ans[5]=check(num,m6); ans[6]=check(num,m7); ans[7]=check(num,m8); Arrays.sort(ans); int max = ans[0]; System.out.println(max); } static int check(int x[],int y[]){ int res=0; for(int i=0;i<9;i++){ res=res+Math.abs(x[i]-y[i]); } return res; } }