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 in = new Scanner(System.in); int[][] square = new int[3][3]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { square[i][j] = in.nextInt(); } } int[] cost = new int[8]; cost[0] = Math.abs(square[0][0] - 8) + Math.abs(square[0][1] - 1) + Math.abs(square[0][2] - 6) + Math.abs(square[1][0] - 3) + Math.abs(square[1][1] - 5) + Math.abs(square[1][2] - 7) + Math.abs(square[2][0] - 4) + Math.abs(square[2][1] - 9) + Math.abs(square[2][2] - 2); cost[1] = Math.abs(square[0][0] - 4) + Math.abs(square[0][1] - 3) + Math.abs(square[0][2] - 8) + Math.abs(square[1][0] - 9) + Math.abs(square[1][1] - 5) + Math.abs(square[1][2] - 1) + Math.abs(square[2][0] - 2) + Math.abs(square[2][1] - 7) + Math.abs(square[2][2] - 6); cost[2] = Math.abs(square[0][0] - 2) + Math.abs(square[0][1] - 9) + Math.abs(square[0][2] - 4) + Math.abs(square[1][0] - 7) + Math.abs(square[1][1] - 5) + Math.abs(square[1][2] - 3) + Math.abs(square[2][0] - 6) + Math.abs(square[2][1] - 1) + Math.abs(square[2][2] - 8); cost[3] = Math.abs(square[0][0] - 6) + Math.abs(square[0][1] - 7) + Math.abs(square[0][2] - 2) + Math.abs(square[1][0] - 1) + Math.abs(square[1][1] - 5) + Math.abs(square[1][2] - 9) + Math.abs(square[2][0] - 8) + Math.abs(square[2][1] - 3) + Math.abs(square[2][2] - 4); cost[4] = Math.abs(square[0][0] - 6) + Math.abs(square[0][1] - 1) + Math.abs(square[0][2] - 8) + Math.abs(square[1][0] - 7) + Math.abs(square[1][1] - 5) + Math.abs(square[1][2] - 3) + Math.abs(square[2][0] - 2) + Math.abs(square[2][1] - 9) + Math.abs(square[2][2] - 4); cost[5] = Math.abs(square[0][0] - 8) + Math.abs(square[0][1] - 3) + Math.abs(square[0][2] - 4) + Math.abs(square[1][0] - 1) + Math.abs(square[1][1] - 5) + Math.abs(square[1][2] - 9) + Math.abs(square[2][0] - 6) + Math.abs(square[2][1] - 7) + Math.abs(square[2][2] - 2); cost[6] = Math.abs(square[0][0] - 4) + Math.abs(square[0][1] - 9) + Math.abs(square[0][2] - 2) + Math.abs(square[1][0] - 3) + Math.abs(square[1][1] - 5) + Math.abs(square[1][2] - 7) + Math.abs(square[2][0] - 8) + Math.abs(square[2][1] - 1) + Math.abs(square[2][2] - 6); cost[7] = Math.abs(square[0][0] - 2) + Math.abs(square[0][1] - 7) + Math.abs(square[0][2] - 6) + Math.abs(square[1][0] - 9) + Math.abs(square[1][1] - 5) + Math.abs(square[1][2] - 1) + Math.abs(square[2][0] - 4) + Math.abs(square[2][1] - 3) + Math.abs(square[2][2] - 8); int min = Integer.MAX_VALUE; for(int i : cost) if (i < min) min = i; System.out.println(min); } }