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 scan = new Scanner(System.in); int[][] square = new int[3][3]; for(int i = 0; i < 3; i++){ String[] elements = scan.nextLine().split(" "); for(int j = 0; j < 3; j++){ square[i][j] = Integer.parseInt(elements[j]); } } scan.close(); int[] magic; int lowestCost; magic = new int[] {4,9,2,3,5,7,8,1,6}; lowestCost = Math.abs(magic[0]-square[0][0]) + Math.abs(magic[1]-square[0][1]) + Math.abs(magic[2]-square[0][2]) + Math.abs(magic[3]-square[1][0]) + Math.abs(magic[4]-square[1][1]) + Math.abs(magic[5]-square[1][2]) + Math.abs(magic[6]-square[2][0]) + Math.abs(magic[7]-square[2][1]) + Math.abs(magic[8]-square[2][2]); for(int i = 0; i < 7; i++){ if(i == 0){ magic = new int[] {2,7,6,9,5,1,4,3,8}; }else if(i == 1){ magic = new int[] {6,1,8,7,5,3,2,9,4}; }else if(i == 2){ magic = new int[] {8,3,4,1,5,9,6,7,2}; }else if(i == 3){ magic = new int[] {2,9,4,7,5,3,6,1,8}; }else if(i == 4){ magic = new int[] {6,7,2,1,5,9,8,3,4}; }else if(i == 5){ magic = new int[] {8,1,6,3,5,7,4,9,2}; }else if(i == 6){ magic = new int[] {4,3,8,9,5,1,2,7,6}; } int cost = Math.abs(magic[0]-square[0][0]) + Math.abs(magic[1]-square[0][1]) + Math.abs(magic[2]-square[0][2]) + Math.abs(magic[3]-square[1][0]) + Math.abs(magic[4]-square[1][1]) + Math.abs(magic[5]-square[1][2]) + Math.abs(magic[6]-square[2][0]) + Math.abs(magic[7]-square[2][1]) + Math.abs(magic[8]-square[2][2]); if(cost < lowestCost){ lowestCost = cost; } } System.out.print(lowestCost); } }