import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { final int N = 3; final int M = 15; Scanner scan = new Scanner(System.in); int[][] s = new int[N][N]; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { s[i][j] = scan.nextInt(); } } final int center = 5; final int[][] borders = {{4,9,2,7,6,1,8,3},{4,3,8,1,6,7,2,9}}; int[] totalCosts = new int[8]; for(int i = 0;i < 8; i++) { int rem = i % 2; totalCosts[i] = Math.abs(s[1][1] - center); totalCosts[i] += Math.abs(s[0][0] - borders[rem][(0+i-rem) % 8]); totalCosts[i] += Math.abs(s[0][1] - borders[rem][(1+i-rem) % 8]); totalCosts[i] += Math.abs(s[0][2] - borders[rem][(2+i-rem) % 8]); totalCosts[i] += Math.abs(s[1][2] - borders[rem][(3+i-rem) % 8]); totalCosts[i] += Math.abs(s[2][2] - borders[rem][(4+i-rem) % 8]); totalCosts[i] += Math.abs(s[2][1] - borders[rem][(5+i-rem) % 8]); totalCosts[i] += Math.abs(s[2][0] - borders[rem][(6+i-rem) % 8]); totalCosts[i] += Math.abs(s[1][0] - borders[rem][(7+i-rem) % 8]); } int minCost = totalCosts[0]; for(int cost : totalCosts) { if(cost < minCost) minCost = cost; } System.out.printf("%d", minCost); } }