#include<bits/stdc++.h>
using namespace std;
#define int long long
int diff[10];
int num[]={1,2,3,4,5,6,7,8,9,10};

bool check(){
  int A[3][3],mod = num[9];
  for(int i=0;i<10;i++)if(diff[i]>0&&num[i]!=diff[i])return 0;
  for(int i=0;i<9;i++) A[i/3][i%3] = num[i];


  int used[20]={};
  for(int i=0;i<3;i++){
    int a=0,b=0;    
    for(int j=0;j<3;j++)a+=A[i][j],b+=A[j][i];
    a%=mod,b%=mod;
    used[a]++;
    used[b]++;
    if(used[a]!=(i+1)*2)return 0;    
  }
  
int a =A[0][0]+A[1][1]+A[2][2],b = A[0][2]+A[1][1]+A[2][0];
used[a%mod]++,used[b%mod]++;
 return used[a%mod] == 8;
}

signed main(){

  while(1){
    int Exit = 1;
    for(int i=0;i<10;i++)cin>>diff[i],Exit*=(diff[i]<0);
    if(Exit) return 0;

  for(int i=0;i<10;i++)num[i] = i+1;

  int ans=0;
  do{
    ans+=check();
  }while(next_permutation(num,num+10));
  cout<<ans<<endl;
  }
  return 0;
}