using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { //Siamese method private static int[,] MagicSquare { get { var matrix = new int[3, 3]; matrix[0, 1] = 1; matrix[2, 2] = 1 + 1; matrix[1, 0] = 1 + 2; matrix[2, 0] = 1 + 3; matrix[1, 1] = 1 + 4; matrix[0, 2] = 1 + 5; matrix[1, 2] = 1 + 6; matrix[0, 0] = 1 + 7; matrix[2, 1] = 1 + 8; return matrix; } } private static int[,] Rotate(int[,] square) { var rotate = new int[3, 3]; rotate[0, 1] = square[0, 0]; rotate[1, 2] = square[0, 1]; rotate[2, 2] = square[0, 2]; rotate[2, 1] = square[1, 2]; rotate[2, 0] = square[2, 2]; rotate[1, 0] = square[2, 1]; rotate[0, 0] = square[2, 0]; rotate[0, 1] = square[1, 0]; rotate[0, 2] = square[0, 0]; rotate[1, 1] = square[1, 1]; return rotate; } private static int[,] Flip(int[,] square) { var flip = new int[3, 3]; for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { flip[i, j] = square[i, 2 - j]; } } return flip; } private static int Cost(int[,] square, int[,] magicSquare) { var cost = 0; for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { cost += Math.Abs(square[i, j] - magicSquare[i, j]); } } return cost; } private static int MinCost(int[,] square) { var magicSquare = MagicSquare; var minCost = Math.Min(int.MaxValue, Cost(square, magicSquare)); for (var i = 0; i < 4; i++) { magicSquare = Flip(magicSquare); minCost = Math.Min(minCost, Cost(square, magicSquare)); magicSquare = Flip(magicSquare); magicSquare = Rotate(magicSquare); minCost = Math.Min(minCost, Cost(square, magicSquare)); } return minCost; } static void Main(string[] args) { var square = new int[3, 3]; for (var i = 0; i < 3; i++) { var values = Console.ReadLine().Split(' ').Select(x => Convert.ToInt32(x)).ToArray(); for (var j = 0; j < 3; j++) { square[i, j] = values[j]; } } Console.WriteLine(MinCost(square)); } }