import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[][] input = new int[3][3]; input[0][0] = scanner.nextInt(); input[0][1] = scanner.nextInt(); input[0][2] = scanner.nextInt(); input[1][0] = scanner.nextInt(); input[1][1] = scanner.nextInt(); input[1][2] = scanner.nextInt(); input[2][0] = scanner.nextInt(); input[2][1] = scanner.nextInt(); input[2][2] = scanner.nextInt(); // try all possible combinations of the nine digits int[][] solution = new int[3][3]; int minDiff = Integer.MAX_VALUE; for (int i1=1; i1<=9; i1++) { for (int i2=1; i2<=9; i2++) { if (i2 == i1) continue; for (int i3=1; i3<=9; i3++) { if (i3 == i1 || i3 == i2) continue; for (int i4=1; i4<=9; i4++) { if (i4 == i1 || i4 == i2 || i4 == i3) continue; // int i7 = i2 + i3 - i4; // if (i7 == i1 || i7 == i2 || i7 == i3 || i7 == i4) continue; for (int i5=1; i5<=9; i5++) { if (i5 == i1 || i5 == i2 || i5 == i3 || i5 == i4) continue; for (int i6=1; i6<=9; i6++) { if (i6 == i1 || i6 == i2 || i6 == i3 || i6 == i4 || i6 == i5) continue; for (int i7=1; i7<=9; i7++) { if (i7 == i1 || i7 == i2 || i7 == i3 || i7 == i4 || i7 == i5 || i7 == i6) continue; for (int i8=1; i8<=9; i8++) { if (i8 == i1 || i8 == i2 || i8 == i3 || i8 == i4 || i8 == i5 || i8 == i6 || i8 == i7) continue; for (int i9=1; i9<=9; i9++) { if (i9 == i1 || i9 == i2 || i9 == i3 || i9 == i4 || i9 == i5 || i9 == i6 || i9 == i7 || i9 == i8) continue; // int i6 = i1 + i2 + i3 - i4 - i5; // if (i6 == i1 || i6 == i2 || i6 == i3 || i6 == i4 || i6 == i5 || i6 == i7) continue; // int i8 = i1 + i4 + i7 - i2 - i5; // if (i8 == i1 || i8 == i2 || i8 == i3 || i8 == i4 || i8 == i5 || i8 == i6 || i8 == i7) continue; // int i9 = i1 + i2 + i3 - i7 - i8; // if (i9 == i1 || i9 == i2 || i9 == i3 || i9 == i4 || i9 == i5 || i9 == i6 || i9 == i7 || i9 == i8) continue; solution[0][0] = i1; solution[0][1] = i2; solution[0][2] = i3; solution[1][0] = i4; solution[1][1] = i5; solution[1][2] = i6; solution[2][0] = i7; solution[2][1] = i8; solution[2][2] = i9; if (!isMagicSquare(solution)) continue; int diff = computeDifference(input, solution); if (diff < minDiff) { minDiff = diff; } } } } } } } } } } System.out.println(minDiff); // check to see if its a magic square // find the distance between this solution and the input // output the solution with the minimum difference } public static boolean isMagicSquare(int[][] numbers) { int sum = numbers[0][0] + numbers[0][1] + numbers[0][2]; if (numbers[1][0] + numbers[1][1] + numbers[1][2] != sum) { return false; } if (numbers[2][0] + numbers[2][1] + numbers[2][2] != sum) { return false; } if (numbers[0][0] + numbers[1][0] + numbers[2][0] != sum) { return false; } if (numbers[0][1] + numbers[1][1] + numbers[2][1] != sum) { return false; } if (numbers[0][2] + numbers[1][2] + numbers[2][2] != sum) { return false; } if (numbers[0][0] + numbers[1][1] + numbers[2][2] != sum) { return false; } if (numbers[0][2] + numbers[1][1] + numbers[2][0] != sum) { return false; } return true; } public static int computeDifference(int[][] base, int[][] other) { int diff = 0; diff += Math.abs(base[0][0] - other[0][0]); diff += Math.abs(base[0][1] - other[0][1]); diff += Math.abs(base[0][2] - other[0][2]); diff += Math.abs(base[1][0] - other[1][0]); diff += Math.abs(base[1][1] - other[1][1]); diff += Math.abs(base[1][2] - other[1][2]); diff += Math.abs(base[2][0] - other[2][0]); diff += Math.abs(base[2][1] - other[2][1]); diff += Math.abs(base[2][2] - other[2][2]); return diff; } }