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;

//Moduic Squares
public class Main{

	int[][] a;
	int M, res;
	boolean[] u;
	
	void f(int k, int m){
		if(k==3){
			dfs(1, 0, m); return;
		}
		if(a[0][k]>0)f(k+1, (m+a[0][k])%M);
		else for(int j=1;j<=10;j++){
			if(!u[j]){
				u[j] = true;
				a[0][k] = j;
				f(k+1, (m+j)%M);
				u[j] = false;
				a[0][k] = 0;
			}
		}
	}
	
	void dfs(int i, int j, int m){
		if(i==3){
			res++; return;
		}
		if(a[i][j]>0){
			boolean f = true;
			if(j==2){
				if((a[i][0]+a[i][1]+a[i][2])%M!=m)f = false;
			}
			if(i==2){
				if((a[0][j]+a[1][j]+a[2][j])%M!=m)f = false;
			}
			if(i==2&&j==0){
				if((a[2][0]+a[1][1]+a[0][2])%M!=m)f = false;
			}
			if(i==2&&j==2){
				if((a[0][0]+a[1][1]+a[2][2])%M!=m)f = false;
			}
			if(f)dfs(i+(j+1)/3, (j+1)%3, m);
		}
		else{
			for(int k=1;k<=10;k++){
				if(!u[k]){
					u[k] = true;
					a[i][j] = k;
					boolean f = true;
					if(j==2){
						if((a[i][0]+a[i][1]+a[i][2])%M!=m)f = false;
					}
					if(i==2){
						if((a[0][j]+a[1][j]+a[2][j])%M!=m)f = false;
					}
					if(i==2&&j==0){
						if((a[2][0]+a[1][1]+a[0][2])%M!=m)f = false;
					}
					if(i==2&&j==2){
						if((a[0][0]+a[1][1]+a[2][2])%M!=m)f = false;
					}
					if(f)dfs(i+(j+1)/3, (j+1)%3, m);
					u[k] = false;
					a[i][j] = 0;
				}
			}
		}
	}
	
	void run(){
		Scanner sc = new Scanner(System.in);
		for(;;){
			a = new int[3][3];
			for(int i=0;i<3;i++)for(int j=0;j<3;j++)a[i][j]=sc.nextInt();
			M = sc.nextInt();
			if(M<0)break;
			res = 0;
			u = new boolean[11];
			for(int i=0;i<3;i++)for(int j=0;j<3;j++)u[a[i][j]] = true;
			u[M] = true;
			if(M>0)f(0, 0);
			else for(int k=1;k<=10;k++){
				if(!u[k]){
					u[k] = true; M = k; f(0, 0); u[k] = false;
				}
			}
			System.out.println(res);
		}
	}
	
	public static void main(String[] args) {
		new Main().run();
	}
}