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 */ var m1 = "8 1 6 3 5 7 4 9 2"; var m2 = "4 3 8 9 5 1 2 7 6"; var m3 = "2 9 4 7 5 3 6 1 8"; var m4 = "6 7 2 1 5 9 8 3 4"; var m5 = "6 1 8 7 5 3 2 9 4"; var m6 = "8 3 4 1 5 9 6 7 2"; var m7 = "4 9 2 3 5 7 8 1 6"; var m8 = "2 7 6 9 5 1 4 3 8"; var l = Console.ReadLine() + " " + Console.ReadLine() + " " + Console.ReadLine(); var min = GetDiff(m1, l); var r = GetDiff(m2, l); if (r < min) min = r; r = GetDiff(m3, l); if (r < min) min = r; r = GetDiff(m4, l); if (r < min) min = r; r = GetDiff(m5, l); if (r < min) min = r; r = GetDiff(m6, l); if (r < min) min = r; r = GetDiff(m7, l); if (r < min) min = r; r = GetDiff(m8, l); if (r < min) min = r; Console.WriteLine(min); } static int GetDiff(string s1, string s2) { var res = 0; for (int i = 0; i < s1.Length; i++) if (s1[i] != s2[i]) res += Math.Abs(int.Parse(s1[i] + "") - int.Parse(s2[i] + "")); return res; } }