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<algorithm>
#include<vector>
#include<bitset>
#include<map>
#include<cassert>
#include<deque>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int ans;
map<vector<int>,bool> exist;
bool check(vector<int>& box);

void dfs(int p,vector<int>& box,int bit)
{
  if(p == 10)
    {
      ans += check(box)?1:0;
      return;
    }
  
  if(box[p])
    {
      dfs(p+1,box,bit);
      return;
    } 
  /*
  if(exist[box])
    return;
  exist[box] = true; 
  */
  for(int j=1;j<=10;j++)
    {
      if((bit >> j) & 1)
	continue;
      box[p] = j;
      dfs(p+1,box,bit|(1<<j));
      box[p] = 0;
    }


}

int main()
{
  while(1)
    {
      vector<int> box(10);
      ans = 0;
      exist.clear();
      int bit = 0;
      bool F = true;
      rep(i,10)
	{
	  cin >> box[i];
	  bit |= (1 << box[i]);
	  if(box[i] != -1)
	    F = false;	  
	}
      
      if(F)
	break;
      bit &= (((1<<20)-1)-1);
      dfs(0,box,bit);
      cout <<ans << endl;
   
    }
  return 0;
}

bool check(vector<int>& g)
{
  int module = g[9]; 
  int sum = (g[0]+g[1]+g[2])%module;
  if((g[3]+g[4]+g[5])%module != sum || (g[6]+g[7]+g[8])%module != sum)
    return false;

  if((g[0]+g[3]+g[6])%module != sum || (g[1]+g[4]+g[7])%module != sum || (g[2]+g[5]+g[8])%module != sum)
    return false;

  if((g[0]+g[4]+g[8])%module != sum || (g[2]+g[4]+g[6])%module != sum)
    return false;

  return true;
  
}