using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ int result = Int32.MaxValue; int[] magicMatrix = new int[9]; int s_index = 0; for (int arr_i = 0; arr_i < 3; arr_i++) { string[] arr_temp = Console.ReadLine().Split(' '); foreach (string s in arr_temp) { magicMatrix[s_index] = Convert.ToInt32(s); s_index++; } //magicMatrix[arr_i] = Array.ConvertAll(arr_temp, Int32.Parse); } int[][] magicCombo = new int[8][]; magicCombo[0] = new int[9] { 8, 1, 6, 3, 5, 7, 4, 9, 2 }; magicCombo[1] = new int[9] { 6, 1, 8, 7, 5, 3, 2, 9, 4 }; magicCombo[2] = new int[9] { 4, 9, 2, 3, 5, 7, 8, 1, 6 }; magicCombo[3] = new int[9] { 2, 9, 4, 7, 5, 3, 6, 1, 8 }; magicCombo[4] = new int[9] { 8, 3, 4, 1, 5, 9, 6, 7, 2 }; magicCombo[5] = new int[9] { 4, 3, 8, 9, 5, 1, 2, 7, 6 }; magicCombo[6] = new int[9] { 6, 7, 2, 1, 5, 9, 8, 3, 4 }; magicCombo[7] = new int[9] { 2, 7, 6, 9, 5, 1, 4, 3, 8 }; for (int i = 0; i < 8; i++) { int cc = 0; for (int j = 0; j < 9; j++) { cc = cc + Math.Abs(magicCombo[i][j] - magicMatrix[j]); } if (result > cc) { result = cc; } } if (result == Int32.MaxValue) { result = 0; } Console.WriteLine(result); } }