import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { int[] m1 = {8,1,6, 3,5,7, 4,9,2}; int[] m2 = {4,3,8, 9,5,1, 2,7,6}; int[] m3 = {2,9,4, 7,5,3, 6,1,8}; int[] m4 = {6,7,2, 1,5,9, 8,3,4}; int[] m5 = {6,1,8, 7,5,3, 2,9,4}; int[] m6 = {8,3,4, 1,5,9, 6,7,2}; int[] m7 = {4,9,2, 3,5,7, 8,1,6}; int[] m8 = {2,7,6, 9,5,1, 4,3,8}; int[] s = new int[9]; Scanner sc = new Scanner(System.in); int[] costs = new int[8]; for(int i = 0; i < 9; i++) { s[i] = sc.nextInt(); } for(int i = 0; i < 9; i++) { costs[0] += Math.abs(s[i] - m1[i]); costs[1] += Math.abs(s[i] - m2[i]); costs[2] += Math.abs(s[i] - m3[i]); costs[3] += Math.abs(s[i] - m4[i]); costs[4] += Math.abs(s[i] - m5[i]); costs[5] += Math.abs(s[i] - m6[i]); costs[6] += Math.abs(s[i] - m7[i]); costs[7] += Math.abs(s[i] - m8[i]); } Arrays.sort(costs); System.out.println(costs[0]); } }