using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { int[] r1 = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse); int[] r2 = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse); int[] r3 = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse); int[][] input = new int[][] { r1, r2, r3 }; List combinations = new List { new int[,] { {8, 1, 6}, {3, 5, 7}, {4, 9, 2} }, new int[,] { {4, 3, 8}, {9, 5, 1}, {2, 7, 6} }, new int[,] { {2, 9, 4}, {7, 5, 3}, {6, 1, 8} }, new int[,] { {6, 7, 2}, {1, 5, 9}, {8, 3, 4} }, new int[,] { {6, 1, 8}, {7, 5, 3}, {2, 9, 4} }, new int[,] { {8, 3, 4}, {1, 5, 9}, {6, 7, 2} }, new int[,] { {4, 9, 2}, {3, 5, 7}, {8, 1, 6} }, new int[,] { {2, 7, 6}, {9, 5, 1}, {4, 3, 8} } }; int minCost = int.MaxValue; foreach(int[,] combi in combinations){ int combCost = 0; for( int i = 0; i < 3; i++){ for ( int j = 0; j < 3; j++) { if ( input[i][j] != combi[i,j]){ combCost += Math.Abs(input[i][j] - combi[i,j]); } } } if ( combCost < minCost ){ minCost = combCost; } } Console.WriteLine(minCost); } }