package no359; import java.util.ArrayList; import java.util.Scanner; import java.util.TreeSet; public class Main { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); for(int tt=0;tt ts = new TreeSet<>(); int[] r = new int[2]; int[] c = new int[2]; int ind = 0; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { int h = sc.nextInt(); a[i][j] = h; if (h == 0) { r[ind] = i; c[ind] = j; ind++; continue; } if (h < l) { ts.add(h); } if (l - h >= 1) { ts.add(l-h); } } } ts.add(0); ts.add(l); ArrayList ev1 = new ArrayList<>(ts); ArrayList ev2 = new ArrayList<>(); for(int i=1;i= 2) { ev2.add(new Pair(h-1, h - h2 - 1)); } } int ans = 0; for(Pair p: ev2) { a[r[0]][c[0]] = p.x; a[r[1]][c[1]] = l - p.x; if (isKadomatsuMatrix(a)) { ans += p.weight; } } System.out.println(ans); } static class Pair { int x; int weight; public Pair(int x, int weight) { super(); this.x = x; this.weight = weight; } } public static boolean isKadomatsuSequence(long a,long b,long c) { if (a == b || b == c || a == c) { return false; } return b < a && b < c || b > a && b > c; } public static boolean isKadomatsuMatrix(int[][] a) { for(int i=0;i<3;i++) { if (!isKadomatsuSequence(a[i][0], a[i][1], a[i][2])) { return false; } if (!isKadomatsuSequence(a[0][i], a[1][i], a[2][i])) { return false; } } return isKadomatsuSequence(a[0][0], a[1][1], a[2][2]) && isKadomatsuSequence(a[0][2], a[1][1], a[2][0]); } }