Problem: Have you ever heard of Moduic Squares? They are like 3 × 3 Magic Squares, but each of them has one extra cell called a moduic cell. Hence a Moduic Square has the following form.

<image>

Figure 1: A Moduic Square

Each of cells labeled from A to J contains one number from 1 to 10, where no two cells contain the same number. The sums of three numbers in the same rows, in the same columns, and in the diagonals in the 3 × 3 cells must be congruent modulo the number in the moduic cell. Here is an example Moduic Square:

<image>

Figure 2: An example Moduic Square

You can easily see that all the sums are congruent to 0 modulo 5.

Now, we can consider interesting puzzles in which we complete Moduic Squares with partially filled cells. For example, we can complete the following square by filling 4 into the empty cell on the left and 9 on the right. Alternatively, we can also complete the square by filling 9 on the left and 4 on the right. So this puzzle has two possible solutions.

<image>

Figure 3: A Moduic Square as a puzzle

Your task is to write a program that determines the number of solutions for each given puzzle.



Input

The input contains multiple test cases. Each test case is specified on a single line made of 10 integers that represent cells A, B, C, D, E, F, G, H, I, and J as shown in the first figure of the problem statement. Positive integer means that the corresponding cell is already filled with the integer. Zero means that the corresponding cell is left empty.

The end of input is identified with a line containing ten of -1’s. This is not part of test cases.

Output

For each test case, output a single integer indicating the number of solutions on a line. There may be cases with no solutions, in which you should output 0 as the number.

Example

Input

3 1 6 8 10 7 0 0 2 5
0 0 0 0 0 0 0 0 0 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1


Output

2
362880

Solution (in JAVA):

Solution:
import java.util.Scanner;

class Main {
    private static boolean isModuicSquare(int[][] table, int moduic) {
        int modulo = (table[0][0] + table[0][1] + table[0][2]) % moduic;
        for (int i = 1; i < 3; i++) {
            if ((table[i][0] + table[i][1] + table[i][2]) % moduic != modulo) {
                return false;
            }
        }
        for (int j = 0; j < 3; j++) {
            if ((table[0][j] + table[1][j] + table[2][j]) % moduic != modulo) {
                return false;
            }
        }
        if ((table[0][0] + table[1][1] + table[2][2]) % moduic != modulo) {
            return false;
        }
        if ((table[2][0] + table[1][1] + table[0][2]) % moduic != modulo) {
            return false;
        }
        return true;
    }

    private static int rec(int n, int[][] table, int moduic, boolean[] used) {
        int x = n / 3;
        int y = n % 3;
        int result = 0;
        if (n > 8) {
            for (int i = 1; i < 11; i++) {
                if (!used[i]) {
                    moduic = i;
                }
            }
            if (isModuicSquare(table, moduic)) {
                result = 1;
            } else {
                result = 0;
            }
        } else if (table[x][y] == 0) {
            for (int i = 1; i < 11; i++) {
                if (!used[i]) {
                    table[x][y] = i;
                    used[i] = true;
                    result += rec(n + 1, table, moduic, used);
                    table[x][y] = 0;
                    used[i] = false;
                }
            }
        } else {
            result = rec(n + 1, table, moduic, used);
        }
        return result;
    }
         
    private static void solve() {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            int[][] table = new int[3][3];
            boolean[] used = new boolean[11];
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    table[i][j] = scanner.nextInt();
                }
            }
            int moduic = scanner.nextInt();
            if (moduic == -1) {
                break;
            }
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    used[table[i][j]] = true;
                }
            }
            used[moduic] = true;
            System.out.println(rec(0, table, moduic, used));
        }
    }

    public static void main(String... args) {
        solve();
    }
}