import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { private static final int[] MAGIC_SQUARE = {4, 9, 2, 3, 5, 7, 8, 1, 6}; private static int[] rotate(int[] square) { return new int[] { square[6], square[3], square[0], square[7], square[4], square[1], square[8], square[5], square[2], }; } private static int[] mirrorH(int[] square) { return new int[] { square[2], square[1], square[0], square[5], square[4], square[3], square[8], square[7], square[6] }; } private static int getCost(int[] a) { int cost = 0; for (int i = 0; i < a.length; i++) { cost += Math.abs(a[i] - MAGIC_SQUARE[i]); } return cost; } private static int getMinRotatedCost(int[] in) { int minCost = Integer.MAX_VALUE; for (int i = 0; i < 4; i++) { int cost = getCost(in); if (cost < minCost) { minCost = cost; } in = rotate(in); } return minCost; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] input = new int[9]; for (int i = 0; i < 9; i++) { input[i] = in.nextInt(); } int minCost = Math.min( getMinRotatedCost(input), getMinRotatedCost(mirrorH(input)) ); System.out.println(minCost); } }