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[] rs = new int[9]; for(int i =0; i < 3;i++) { var strline = Console.In.ReadLine(); var splits = strline.Split(' '); for(int j = 0 ; j < splits.Length;j++) { rs[3*i+j] = int.Parse(splits[j]); } } int[][] targuets = new int[8][]; targuets[0] = GetVariant(8,1,6,3,5,7,4,9,2); targuets[1] = GetVariant(6,1,8,7,5,3,2,9,4); targuets[2] = GetVariant(8,3,4,1,5,9,6,7,2); targuets[3] = GetVariant(6,7,2,1,5,9,8,3,4); targuets[4] = GetVariant(4,9,2,3,5,7,8,1,6); targuets[5] = GetVariant(2,9,4,7,5,3,6,1,8); targuets[6] = GetVariant(4,3,8,9,5,1,2,7,6); targuets[7] = GetVariant(2,7,6,9,5,1,4,3,8); int min = int.MaxValue; for(int i =0; i < targuets.Length;i++) { int currentdif = 0; for(int j =0; j < 9;j++) { currentdif = currentdif + Math.Abs(targuets[i][j] - rs[j]); } if (currentdif < min) { min = currentdif; } } Console.Out.WriteLine(min); } public static int[] GetVariant(int p1, int p2,int p3, int p4, int p5, int p6, int p7, int p8, int p9) { int[] targuets = new int[9]; targuets[0] = p1;targuets[1] = p2;targuets[2] = p3; targuets[3] = p4;targuets[4] = p5;targuets[5] = p6; targuets[6] = p7;targuets[7] = p8;targuets[8] = p9; return targuets; } }