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 */ List lst = new List(); lst.AddRange(Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse)); lst.AddRange(Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse)); lst.AddRange(Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse)); Console.WriteLine(GetMagicCost(lst.ToArray())); } static int GetMagicCost(int[] arr) { List magicSquares = new List() { new int[] {8,1,6,3,5,7,4,9,2}, new int[] {6,1,8,7,5,3,2,9,4}, new int[] {4,3,8,9,5,1,2,7,6}, new int[] {2,7,6,9,5,1,4,3,8}, new int[] {2,9,4,7,5,3,6,1,8}, new int[] {4,9,2,3,5,7,8,1,6}, new int[] {6,7,2,1,5,9,8,3,4}, new int[] {8,3,4,1,5,9,6,7,2}, }; int max = int.MaxValue; foreach (var item in magicSquares) { int cost = 0; for (int i = 0; i < 9; i++) { cost += Math.Abs(arr[i] - item[i]); } if (cost == 0) { return 0; } if (cost < max) { max = cost; } } return max; } }