using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { int n=3; var n1= Console.ReadLine().Split(' '); var n2= Console.ReadLine().Split(' '); var n3= Console.ReadLine().Split(' '); int[][] arr = new int[3][] { Array.ConvertAll(n1, s => Int32.Parse(s)), Array.ConvertAll(n2, s => Int32.Parse(s)), Array.ConvertAll(n3, s => Int32.Parse(s)) }; int[][][] s1 = new int[8][][] { new int[3][]{ new int[] { 8, 1, 6 }, new int[] { 3, 5, 7 }, new int[] { 4, 9, 2 } } , new int[3][] { new int[] { 6,1,8 }, new int[] { 7,5,3 }, new int[] { 2,9,4 } } ,new int[3][] { new int[] { 4,9,2 }, new int[] { 3, 5, 7 }, new int[] { 8,1,6 } } , new int[3][] { new int[] { 2,9,4 }, new int[] { 7,5,3 }, new int[] {6,1,8 } } , new int[3][] { new int[] { 8, 3,4 }, new int[] { 1,5,9 }, new int[] { 6,7,2 } } , new int[3][] { new int[] { 4,3,8 }, new int[] { 9,5,1 }, new int[] { 2,7,6 } } , new int[3][] { new int[] { 6,7,2 }, new int[] { 1,5,9 }, new int[] { 8,3,4 } } , new int[3][] { new int[] { 2,7,6 }, new int[] { 9,5,1 }, new int[] {4,3,8 } } } ; int[] cost =new int[8]; var index = 8; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int x = 0; x < index; x++) { cost[x] += Math.Abs(arr[i][j] - s1[x][i][j]); } } } Console.WriteLine(cost.Min()); } }