using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int [,]values = new int [8, 8] { {4,9,2,7,6,1,8,3}, {4,3,8,1,6,7,2,9}, {2,9,4,3,8,1,6,7}, {2,7,6,1,8,3,4,9}, {8,3,4,9,2,7,6,1}, {8,1,6,7,2,9,4,3}, {6,1,8,3,4,9,2,7}, {6,7,2,9,4,3,8,1} }; int [,] matrix = new int[3,3]; for(int i = 0; i < 3; i++) { int[] array = Console.ReadLine().Split(new char[] {' '}).Select(t => Int32.Parse(t)).ToArray(); matrix[i, 0] = array[0]; matrix[i, 1] = array[1]; matrix[i, 2] = array[2]; } int cost = 0; if (matrix[1,1] != 5) { cost += Math.Abs(matrix[1,1] - 5); } int [] strip = new int [] { matrix[0,0], matrix[0,1], matrix[0,2], matrix[1,2], matrix[2,2], matrix[2,1], matrix[2,0], matrix[1,0] }; int min = 100; for (int i = 0; i < 8; i++) { int tempCost = 0; for(int j = 0; j < 8; j++) tempCost += Math.Abs(strip[j] - values[i,j]); if (tempCost < min) min = tempCost; } Console.WriteLine(min + cost); } }