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;

typedef pair<int,int> P;

int in[3][3],mod;
vector<int> V;
vector<P> empty;
bool used[11];
int ans;

void init(){
  V.clear();
  empty.clear();
  fill(used,used+11,false);
  ans = 0;
}

int check_row(){
  int sum[3];
  
  for(int i = 0; i < 3; i++){
    int tmp = 0;
    for(int j = 0; j < 3; j++)
      tmp += in[i][j];
    sum[i] = tmp;
  }

 for(int i = 0; i < 3; i++)
   sum[i] %= mod;


 if((sum[0] == sum[1])&&(sum[0] == sum[2])) return sum[0];
 return -1;
}

int check_column(){
  int sum[3];
  
  for(int i = 0; i < 3; i++){
    int tmp = 0;
    for(int j = 0; j < 3; j++)
      tmp += in[j][i];
    sum[i] = tmp;
  }

  for(int i = 0; i < 3; i++)
    sum[i] %= mod;

  if((sum[0] == sum[1])&&(sum[0] == sum[2])) return sum[0];
  return -1;
}

int check_dia(){
  int sum[2] = {0,0};

  for(int i = 0; i < 3; i++){
    sum[0]+=in[i][i];
    sum[1]+=in[i][2-i];
  }

  sum[0] %= mod;
  sum[1] %= mod;
  
  if(sum[0] == sum[1]) return sum[0];
  return -1;
}

bool check(){

  int ansmod[3];

  if((ansmod[0] = check_row()) == -1) return false;
  
  if((ansmod[1] = check_column()) == -1) return false;
  if((ansmod[2] = check_dia()) == -1) return false;

  
  return (ansmod[0] == ansmod[1] && ansmod[0] == ansmod[2]);
}

void print(){
  for(int i = 0; i < 3; i++){
    for(int j = 0; j < 3; j++)
      cout << in[i][j] << " ";
    cout << endl;
  }
  cout << "mod " << mod << endl << endl;

}

void solve(){

  bool modchange = false;

  if(mod == 0) modchange = true;


  do{
    
    
    if(modchange){
      for(int i = 0; i < V.size()-1; i++)
	in[empty[i].first][empty[i].second] = V[i];
      
      mod = V[V.size()-1];

     

    }else{
      for(int i = 0; i < V.size(); i++)
	in[empty[i].first][empty[i].second] = V[i]; 
    }

    //print();

    if(check()) ans++;

  }while(next_permutation(V.begin(),V.end()));

  cout << ans << endl;
}

bool input(){
  init();
  
  bool flg = false;

  for(int i = 0; i < 3; i++)
    for(int j = 0; j < 3; j++){
      cin  >> in[i][j];
      if(in[i][j] == 0) empty.push_back(P(i,j));
      if(in[i][j] != -1) flg = used[in[i][j]] = true;
    }

  cin >> mod;

  if(mod >= 0) used[mod] = true;

  for(int i = 1; i < 11; i++)
    if(!used[i]) V.push_back(i);

  return flg;
}

int main(){
  
  while(input()) solve();

  return 0;
}