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.Arrays;
import java.util.Scanner;

public class Main {
	Scanner sc = new Scanner(System.in);
	int zero_cnt;
	int[] number;
	int[] pos;
	int[] set;
	int ans;

	void run() {
		for (;;) {
			number = new int[10];
			int[] check = new int[11];
			zero_cnt = 0;
			ans = 0;
			for (int i = 0; i < 10; i++) {
				number[i] = sc.nextInt();
				if (number[i] != -1) {
					check[number[i]]++;
				}
				if (number[i] == 0) {
					zero_cnt++;
				}
			}
			if (number[0] == -1) {
				return;
			}
			set = new int[zero_cnt];
			pos = new int[zero_cnt];
			int index = 0;
			int index2 = 0;
			for (int i = 1; i <= 10; i++) {
				if (check[i] == 0) {
					set[index++] = i;
				}
			}
			for (int i = 0; i < 10; i++) {
				if (number[i] == 0) {
					pos[index2++] = i;
				}
			}
			boolean[] use = new boolean[zero_cnt];
			solve(0, use);
			System.out.println(ans);
		}
	}

	void solve(int n, boolean[] use) {
		if (n == zero_cnt) {
			judge(number);
		} else {
			for (int i = 0; i < zero_cnt; i++) {
				if (use[i]) {
					continue;
				}
				number[pos[i]] = set[n];
				use[i] = true;
				solve(n + 1, use);
				use[i] = false;
			}
		}
	}

	void judge(int[] res) {
		int rest = (res[0]+res[1] + res[2]) % res[9];
		if ((res[0] + res[1] + res[2]) % res[9] == rest
				&& (res[3] + res[4] + res[5]) % res[9] == rest
				&& (res[6] + res[7] + res[8]) % res[9] == rest
				&& (res[0] + res[3] + res[6]) % res[9] == rest
				&& (res[1] + res[4] + res[7]) % res[9] == rest
				&& (res[2] + res[5] + res[8]) % res[9] == rest
				&& (res[0] + res[4] + res[8]) % res[9] == rest
				&& (res[2] + res[4] + res[6]) % res[9] == rest) {
			ans++;
		}
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
}