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 <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;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()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int R, M;
vector<int> calc(int a[3][3]) {
  vector<int> res;
  REP(i, 3) {
    int sum = 0;
    REP(j, 3) sum += a[i][j];
    res.push_back(sum%M);
  }
  REP(j, 3) {
    int sum = 0;
    REP(i, 3) sum += a[i][j];
    res.push_back(sum%M);
  }
  REP(k, 2) {
    int sum = 0;
    REP(i, 3) {
      int j;
      if (k) j = 2-i;
      else j = i;
      sum += a[i][j];
    }
    res.push_back(sum%M);
  }
  return res;
}
int a[3][3];
int dfs(int y, int x, int S) {
  if (y == 3) {
    // REP(i, 3) {
    //   REP(j, 3) {
    //     cout << a[i][j] << " ";
    //   }
    //   cout << endl;
    // }
    vector<int> v = calc(a);
    int hoge = v[0];
    for (int i=1; i<v.size(); ++i) {
      if (hoge != v[i]) return 0;
    }
    return 1;
  }
  if (x == 3) return dfs(y+1, 0, S);
  if (a[y][x] != 0) return dfs(y, x+1, S);
  int res = 0;
  for (int i=1; i<=10; ++i) {
    if (S>>i&1) continue;
    a[y][x] = i;
    res += dfs(y, x+1, S|1<<i);
  }
  a[y][x] = 0;
  return res;
}

int main() {
  while(1) {
    int S = 0;
    REP(i, 3) REP(j, 3) {
      cin >> a[i][j];
        S |= 1<<(a[i][j]);
    }
    cin >> M;
    S |= 1<<M;
    if (a[0][0] == -1) break;
    int res = 0;
    if (M == 0) {
      for (int i=1; i<=10; ++i) {
        if (S>>i&1) continue;
        M = i;
        res += dfs(0, 0, S|1<<i);
      }
    } else {
      res = dfs(0,0,S);
    }
    cout << res << endl;
  }
}