import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); ArrayList l = new ArrayList<>(); int[][] magic1 = { {4, 9, 2}, {3, 5, 7}, {8, 1, 6} }; l.add(magic1); int[][] magic2 = { {8, 3, 4}, {1, 5, 9}, {6, 7, 2} }; l.add(magic2); int[][] magic3 = { {8, 1, 6}, {3, 5, 7}, {4, 9, 2} }; l.add(magic3); int[][] magic4 = { {6, 7, 2}, {1, 5, 9}, {8, 3, 4} }; l.add(magic4); for(int k=0;k<4;k++){ int[][] mtr = l.get(k); int[][] new_m = new int[3][3]; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ new_m[i][j] = mtr[i][2-j]; } } l.add(new_m); } int[][] m = new int[3][3]; int min = Integer.MAX_VALUE; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { m[i][j] = sc.nextInt(); } } for (int k = 0; k < l.size(); k++) { int dif = 0; int[][] magic = l.get(k); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { dif += Math.abs(m[i][j] - magic[i][j]); } } if (min > dif) { min = dif; } } System.out.println(min); } }