import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner kb = new Scanner(System.in); int arr[][] = new int[3][3]; int tst[][][] = new int[8][3][3]; for(int i = 0;i < 3;i++){ for(int j = 0;j < 3;j++){ arr[i][j] = kb.nextInt(); } } tst[0][0][0] = 8; tst[0][0][1] = 1; tst[0][0][2] = 6; tst[0][1][0] = 3; tst[0][1][1] = 5; tst[0][1][2] = 7; tst[0][2][0] = 4; tst[0][2][1] = 9; tst[0][2][2] = 2; tst[1][0][0] = 4; tst[1][0][1] = 3; tst[1][0][2] = 8; tst[1][1][0] = 9; tst[1][1][1] = 5; tst[1][1][2] = 1; tst[1][2][0] = 2; tst[1][2][1] = 7; tst[1][2][2] = 6; tst[2][0][0] = 2; tst[2][0][1] = 7; tst[2][0][2] = 6; tst[2][1][0] = 9; tst[2][1][1] = 5; tst[2][1][2] = 1; tst[2][2][0] = 4; tst[2][2][1] = 3; tst[2][2][2] = 8; tst[3][0][0] = 2; tst[3][0][1] = 9; tst[3][0][2] = 4; tst[3][1][0] = 7; tst[3][1][1] = 5; tst[3][1][2] = 3; tst[3][2][0] = 6; tst[3][2][1] = 1; tst[3][2][2] = 8; tst[4][0][0] = 6; tst[4][0][1] = 7; tst[4][0][2] = 2; tst[4][1][0] = 1; tst[4][1][1] = 5; tst[4][1][2] = 9; tst[4][2][0] = 8; tst[4][2][1] = 3; tst[4][2][2] = 4; tst[5][0][0] = 4; tst[5][0][1] = 9; tst[5][0][2] = 2; tst[5][1][0] = 3; tst[5][1][1] = 5; tst[5][1][2] = 7; tst[5][2][0] = 8; tst[5][2][1] = 1; tst[5][2][2] = 6; tst[6][0][0] = 6; tst[6][0][1] = 1; tst[6][0][2] = 8; tst[6][1][0] = 7; tst[6][1][1] = 5; tst[6][1][2] = 3; tst[6][2][0] = 2; tst[6][2][1] = 9; tst[6][2][2] = 4; tst[7][0][0] = 8; tst[7][0][1] = 3; tst[7][0][2] = 4; tst[7][1][0] = 1; tst[7][1][1] = 5; tst[7][1][2] = 9; tst[7][2][0] = 6; tst[7][2][1] = 7; tst[7][2][2] = 2; int min = 9999; for(int i = 0;i < 8;i++){ int tmp = 0; for(int j = 0;j < 3;j++){ for(int k = 0;k < 3;k++){ tmp += Math.abs(arr[j][k] - tst[i][j][k]); } } if(tmp < min){ min = tmp; } } System.out.println(min); } }