using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { string[] strArray = Console.ReadLine().Split(); List strList = strArray.ToList(); strArray = Console.ReadLine().Split(); strList.AddRange(strArray); strArray = Console.ReadLine().Split(); strList.AddRange(strArray); List intList = strList.ConvertAll(s => Int32.Parse(s)); // All possible combinations List L1 = new List() { 8, 1, 6, 3, 5, 7, 4, 9, 2 }; List L2 = new List() { 6, 1, 8, 7, 5, 3, 2, 9, 4 }; List L3 = new List() { 4, 9, 2, 3, 5, 7, 8, 1, 6 }; List L4 = new List() { 2, 9, 4, 7, 5, 3, 6, 1, 8 }; List L5 = new List() { 8, 3, 4, 1, 5, 9, 6, 7, 2 }; List L6 = new List() { 4, 3, 8, 9, 5, 1, 2, 7, 6 }; List L7 = new List() { 6, 7, 2, 1, 5, 9, 8, 3, 4 }; List L8 = new List() { 2, 7, 6, 9, 5, 1, 4, 3, 8 }; List> combList = new List>(){L1, L2, L3, L4, L5, L6, L7, L8}; int minCost = int.MaxValue; // Start comparing for (int i = 0; i < combList.Count; i++) { int cost = 0; for (int j = 0; j < combList[i].Count; j++) { cost += Math.Abs(intList[j] - combList[i][j]); } if (cost < minCost) minCost = cost; } Console.WriteLine(minCost); } }