using System; using System.Collections.Generic; using System.IO; using System.Linq; 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 */ var magicalSquare = new int[3, 3]; magicalSquare[0, 0] = 4; magicalSquare[0, 1] = 9; magicalSquare[0, 2] = 2; magicalSquare[1, 0] = 3; magicalSquare[1, 1] = 5; magicalSquare[1, 2] = 7; magicalSquare[2, 0] = 8; magicalSquare[2, 1] = 1; magicalSquare[2, 2] = 6; var square = new int[3][]; square[0] = Console.ReadLine().Split(' ').Select(c => int.Parse(c)).ToArray(); square[1] = Console.ReadLine().Split(' ').Select(c => int.Parse(c)).ToArray(); square[2] = Console.ReadLine().Split(' ').Select(c => int.Parse(c)).ToArray(); var answer = int.MaxValue; for (var i = 0; i < 8; i++) { var sumansw = 0; for (var j = 0; j < 3; j++) { for (var m = 0; m < 3; m++) { sumansw += Math.Abs(square[j][m] - magicalSquare[j, m]); } } if (sumansw < answer) answer = sumansw; if (i < 3) { var aux = magicalSquare[0, 1]; magicalSquare[0, 1] = magicalSquare[1, 0]; var aux1 = aux; aux = magicalSquare[1, 2]; magicalSquare[1, 2] = aux1; aux1 = aux; aux = magicalSquare[2, 1]; magicalSquare[2, 1] = aux1; magicalSquare[1, 0] = aux; aux = magicalSquare[0, 2]; magicalSquare[0, 2] = magicalSquare[0, 0]; aux1 = aux; aux = magicalSquare[2, 2]; magicalSquare[2, 2] = aux1; aux1 = aux; aux = magicalSquare[2, 0]; magicalSquare[2, 0] = aux1; aux1 = aux; aux = magicalSquare[0, 0]; magicalSquare[0, 0] = aux1; } else { var aux = 0; var aux1 = 0; if (i == 3) { aux = magicalSquare[0, 1]; magicalSquare[0, 1] = magicalSquare[1, 0]; aux1 = aux; aux = magicalSquare[1, 2]; magicalSquare[1, 2] = aux1; aux1 = aux; aux = magicalSquare[2, 1]; magicalSquare[2, 1] = aux1; magicalSquare[1, 0] = aux; aux = magicalSquare[0, 2]; magicalSquare[0, 2] = magicalSquare[0, 0]; aux1 = aux; aux = magicalSquare[2, 2]; magicalSquare[2, 2] = aux1; aux1 = aux; aux = magicalSquare[2, 0]; magicalSquare[2, 0] = aux1; aux1 = aux; aux = magicalSquare[0, 0]; magicalSquare[0, 0] = aux1; aux = magicalSquare[0, 0]; magicalSquare[0, 0] = magicalSquare[2, 0]; magicalSquare[2, 0] = aux; aux = magicalSquare[0, 1]; magicalSquare[0, 1] = magicalSquare[2, 1]; magicalSquare[2,1 ] = aux; aux = magicalSquare[2, 2]; magicalSquare[2, 2] = magicalSquare[0, 2]; magicalSquare[0, 2] = aux; continue; } aux = magicalSquare[0, 1]; magicalSquare[0, 1] = magicalSquare[1, 0]; aux1 = aux; aux = magicalSquare[1, 2]; magicalSquare[1, 2] = aux1; aux1 = aux; aux = magicalSquare[2, 1]; magicalSquare[2, 1] = aux1; magicalSquare[1, 0] = aux; aux = magicalSquare[0, 2]; magicalSquare[0, 2] = magicalSquare[0, 0]; aux1 = aux; aux = magicalSquare[2, 2]; magicalSquare[2, 2] = aux1; aux1 = aux; aux = magicalSquare[2, 0]; magicalSquare[2, 0] = aux1; aux1 = aux; aux = magicalSquare[0, 0]; magicalSquare[0, 0] = aux1; } } Console.WriteLine(answer); } }