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 in = new Scanner(System.in); int[] a = new int[3]; int[] b = new int[3]; int[] c = new int[3]; int[][] all = new int[3][3]; for (int a_i = 0; a_i < 3; a_i++) { a[a_i] = in.nextInt(); all[0][a_i] = a[a_i]; } for (int b_i = 0; b_i < 3; b_i++) { b[b_i] = in.nextInt(); all[1][b_i] = b[b_i]; } for (int c_i = 0; c_i < 3; c_i++) { c[c_i] = in.nextInt(); all[2][c_i] = c[c_i]; } if (all[0][0] + all[0][1] + all[0][2] == 3 * all[1][1] && all[1][0] + all[1][1] + all[1][2] == 3 * all[1][1] && all[2][0] + all[2][1] + all[2][2] == 3 * all[1][1] && all[0][0] + all[1][0] + all[2][0] == 3 * all[1][1] && all[0][1] + all[1][1] + all[2][1] == 3 * all[1][1] && all[0][2] + all[1][2] + all[2][2] == 3 * all[1][1] && all[0][0] + all[1][1] + all[2][2] == 3 * all[1][1] && all[0][2] + all[1][1] + all[2][0] == 3 * all[1][1]) { System.out.println("0"); } else { int[] cost = new int[8]; cost[0] = Math.abs(all[0][0] - 8) + Math.abs(all[0][1] - 1) + Math.abs(all[0][2] - 6) + Math.abs(all[1][0] - 3) + Math.abs(all[1][1] - 5) + Math.abs(all[1][2] - 7) + Math.abs(all[2][0] - 4) + Math.abs(all[2][1] - 9) + Math.abs(all[2][2] - 2); cost[1] = Math.abs(all[0][0] - 4) + Math.abs(all[0][1] - 3) + Math.abs(all[0][2] - 8) + Math.abs(all[1][0] - 9) + Math.abs(all[1][1] - 5) + Math.abs(all[1][2] - 1) + Math.abs(all[2][0] - 2) + Math.abs(all[2][1] - 7) + Math.abs(all[2][2] - 6); cost[2] = Math.abs(all[0][0] - 2) + Math.abs(all[0][1] - 9) + Math.abs(all[0][2] - 4) + Math.abs(all[1][0] - 7) + Math.abs(all[1][1] - 5) + Math.abs(all[1][2] - 3) + Math.abs(all[2][0] - 6) + Math.abs(all[2][1] - 1) + Math.abs(all[2][2] - 8); cost[3] = Math.abs(all[0][0] - 6) + Math.abs(all[0][1] - 7) + Math.abs(all[0][2] - 2) + Math.abs(all[1][0] - 1) + Math.abs(all[1][1] - 5) + Math.abs(all[1][2] - 9) + Math.abs(all[2][0] - 8) + Math.abs(all[2][1] - 3) + Math.abs(all[2][2] - 4); cost[4] = Math.abs(all[0][0] - 6) + Math.abs(all[0][1] - 1) + Math.abs(all[0][2] - 8) + Math.abs(all[1][0] - 7) + Math.abs(all[1][1] - 5) + Math.abs(all[1][2] - 3) + Math.abs(all[2][0] - 2) + Math.abs(all[2][1] - 9) + Math.abs(all[2][2] - 4); cost[5] = Math.abs(all[0][0] - 8) + Math.abs(all[0][1] - 3) + Math.abs(all[0][2] - 4) + Math.abs(all[1][0] - 1) + Math.abs(all[1][1] - 5) + Math.abs(all[1][2] - 9) + Math.abs(all[2][0] - 6) + Math.abs(all[2][1] - 7) + Math.abs(all[2][2] - 2); cost[6] = Math.abs(all[0][0] - 4) + Math.abs(all[0][1] - 9) + Math.abs(all[0][2] - 2) + Math.abs(all[1][0] - 3) + Math.abs(all[1][1] - 5) + Math.abs(all[1][2] - 7) + Math.abs(all[2][0] - 8) + Math.abs(all[2][1] - 1) + Math.abs(all[2][2] - 6); cost[7] = Math.abs(all[0][0] - 2) + Math.abs(all[0][1] - 7) + Math.abs(all[0][2] - 6) + Math.abs(all[1][0] - 9) + Math.abs(all[1][1] - 5) + Math.abs(all[1][2] - 1) + Math.abs(all[2][0] - 4) + Math.abs(all[2][1] - 3) + Math.abs(all[2][2] - 8); int lowest = cost[0]; for (int current : cost) { if (current < lowest) { lowest = current; } } System.out.println(lowest); } } }