using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { var matrix = new int[3,3]; for(int i = 0; i < 3; ++i) { var list = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse); for(int k = 0; k < 3; ++k) { matrix[i,k] = list[k]; } } var corner = new int[]{2, 4, 6, 8 }; var sample = new int[3,3]; sample[1,1] = 5; int minDiff = int.MaxValue; for(int i = 0 ; i < corner.Length; ++i) { int diff = Math.Abs(corner[i] - 5); for(int k = 0; k < 2; ++k) { sample[0,0] = corner[i]; sample[2,2] = 10 - sample[0,0]; if(diff == 1) { sample[0, 2] = k == 0 ? 8 : 2; } else { sample[0, 2] = k == 0 ? 4 : 6; } sample[0,1] = 15 - sample[0,0] - sample[0,2]; sample[2,1] = 15 - sample[1,1] - sample[0,1]; sample[2,0] = 15 - sample[2,1] - sample[2,2]; sample[1,0] = 15 - sample[0,0] - sample[2,0]; sample[1,2] = 15 - sample[1,1] - sample[1,0]; int change = 0; for(int y = 0; y < 3; ++y) { for(int x = 0; x < 3; ++x) { change += Math.Abs(matrix[y,x] - sample[y,x]); } } minDiff = Math.Min(change, minDiff); } } Console.WriteLine(minDiff); } }