#include <iostream>
#include <vector>
#include <cassert>
using namespace std;

bool isvalid(const vector<int> &b,int m) {
    int target = -1;
    for(int i=0; i<3; ++i) {
        int t1 = 0,t2 = 0;
        for(int j=0; j<3; ++j) {
            t1 += b[i*3+j];
            t2 += b[j*3+i];
        }
        t1 %= m,t2 %= m;
        if(target == -1) target = t1;
        if(t1 != target || t2 != target) return false;
    }

    int tt = b[0] + b[4] + b[8],tt2 = b[2] + b[4] + b[6];

    tt %= m,tt2 %= m;
    if(tt != target || tt2 != target) return false;

    return true;
}

int dfs(vector<int> &b,int used,int remain) {
    if(remain == 0)
        return (isvalid(b,b[9])) ? 1:0;

    int next = 0, ret = 0;
    while(b[next] != 0) next++;

    for(int use=1; use<=10; ++use) {
        if((used>>use)&1) continue;
        b[next] = use;
        ret += dfs(b,used|(1<<use), remain-1);
        b[next] = 0;
    }

    return ret;
}

int main() {
    vector<int> b(10,0);
    while(true) {
        for(int i=0; i<10; ++i) cin>>b[i];
        for(int i=0; i<10; ++i) {
            if(b[i] != -1) break;
            if(i == 9) goto END;
        }

        int used = 1,rem = 0;
        for(int i=0; i<10; ++i) {
            used |= (1<<(b[i]));
            if(b[i]==0) rem++;
        }

        cout<<dfs(b,used,rem)<<endl;
    }
END:;
}