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 CPP):

Solution:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;



int p[11] = {};
int can[11] = {};
int m;

int check(int t){
	if( t < 4 ) return true;
	//if( t == 4 ) return (p[1]+p[4]+p[7]) % p[9] == 0;
	int c = (p[1]+p[4]+p[7]) % p[9];
	if( t == 5 ) return (p[0]+p[4]+p[8]) % p[9] == c;
	if( t == 6 ) return (p[0]+p[1]+p[2]) % p[9] == c;
	if( t == 7 ) return (p[2]+p[5]+p[8]) % p[9] == c;
	if( t == 8 ) return (p[6]+p[7]+p[8]) % p[9] == c && (p[2]+p[4]+p[6]) % p[9] == c;
	if( t == 9 ) return (p[0]+p[3]+p[6]) % p[9] == c && (p[3]+p[4]+p[5]) % p[9] == c;
	return true;
}
//

int d[] = {9,0,1,4,7,8,2,5,6,3};

int solve(int t){
	/*cout << t << endl;
	cout << "[" << p[9] << "]" << endl;
	for(int i = 0 ; i < 3 ; i++){
		for(int j = 0 ; j < 3 ; j++) cout << p[i*3+j] << " ";
		cout << endl;
	}
	*/
	if( !check(t-1) ) return 0;
	if( t == 10 ){
		return 1;
	}
	int x = d[t];
	if( p[x] == 0 ){
		int ans = 0;
		for(int i = 1 ; i <= 10 ; i++){
			if( can[i] ){
				can[i] = false;
				p[x] = i;
				ans += solve(t+1);
				p[x] = 0;
				can[i] = true;
			}
		}
		return ans;
	}else{
		return solve(t+1);
		
	}
}

int main(){
	while(1){
		for(int i = 0 ; i < 11 ; i++) can[i] = true;
		
		for(int i = 0 ; i < 10 ; i++){
			cin >> p[i];
			can[p[i]] = false;
		}
		if(p[0]==-1)break;
		cout << solve(0) << endl;
	}
}