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<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int p[10];

bool valid(){
  int m = p[9];
  int ok;
  int sum;
  sum = p[0] + p[4] + p[8];
  if(sum % m == (p[2] + p[4] + p[6]) % m){
    ok = sum % m;
  }else{
    return false;
  }

  int sumT[3] = {0};
  int sumY[3] = {0};
  REP(i,3){
    REP(j,3){
      sumY[i] += p[3*i + j];
      sumT[i] += p[3*j + i];
    }
  }
  REP(i,3){
    if(sumT[i] % m != ok) return false;
    if(sumY[i] % m != ok) return false;
  }

  return true;
}

int solve(int now, int f){
  if(now == 10){
    //REP(i,10) printf("%d ",p[i]); puts("");
    return valid() ? 1 : 0;
  }
  if(p[now] == 0){
    int ret = 0;
    REP(i,10) if(f & (1<<i)){
      p[now] = i+1;
      ret += solve(now+1, f^(1<<i));
      p[now] = 0;
    }
    return ret;
  }else{
    int tmp = p[now]-1;
    if(!(f & (1 << tmp))) return 0;
    return solve(now+1, f^(1<<tmp));
  }
}

int main(){
  while(true){
    REP(i,10) scanf("%d",p+i);
    if(p[0] == -1) break;
    print(solve(0, (1<<10)-1));
  }
  return 0;
}