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) { Scanner input = new Scanner(System.in); int[] inSquare = new int[9]; for(int i = 0; i < 9; i++){ inSquare[i] = input.nextInt(); } int[][] squares = new int[][]{{8,1,6,3,5,7,4,9,2,0}, {4,3,8,9,5,1,2,7,6,0}, {2,9,4,7,5,3,6,1,8,0}, {6,7,2,1,5,9,8,3,4,0}, {4,9,2,3,5,7,8,1,6,0}, {8,3,4,1,5,9,6,7,2,0}, {6,1,8,7,5,3,2,9,4,0}, {2,7,6,9,5,1,4,3,8,0}}; for(int a = 0; a < 8; a++){ for(int i = 0; i < 9; i++){ squares[a][9] += Math.abs(squares[a][i] - inSquare[i]); } } int cost = squares[0][9]; for(int k = 1; k < 8; k++){ if(squares[k][9] < cost){ cost = squares[k][9]; } } System.out.println(cost); } }