RT
#include<bits/stdc++.h>
using namespace std;
int a[10][10];
int b[10][10];
long long ans=0;
int n;
bool cmp(int a[],int b[]){
}
bool check(int arr[]){
for(int i=0;i<n;i++){
int cmp=0;
for(int j=0;j<5;j++){
// cout<<arr[j]<<a[i][j]<<"fff"<<endl;
if(arr[j]!=a[i][j]){
cmp++;
// cout<<"eee"<<cmp<<endl;
}
if(j==4&&!cmp){
return false;
}
}
if(cmp==1){
continue;
}
if(cmp>2){
return false;
}
// int pd=0;
for(int j=0;j<4;j++){
if(arr[j]-a[i][j]==arr[j+1]-a[i][j+1]&&arr[j]-a[i][j]!=0){
break;
}
if(j==3){
return false;
}
}
}
// cout<<endl;
return true;
}
int main(){
// freopen("lock.in","r",stdin);
// freopen("lock.out","w",stdout);
scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<5;j++){
scanf("%d",&a[i][j]);
b[i][j]=a[i][j];
}
}
if(n==1){
printf("81");
}else{
for(int i=0;i<5;i++){
for(int j=1;j<=9;j++){
b[0][i]+=j;
b[0][i]%=10;
if(check(b[0])){
ans++;
}
if(b[0][i]<j){
b[0][i]+=10-j;
}else{
b[0][i]-=j;
}
}
}
for(int i=0;i<4;i++){
for(int j=1;j<=9;j++){
b[0][i]+=j;
b[0][i]%=10;
b[0][i+1]+=j;
b[0][i+1]%=10;
if(check(b[0])){
ans++;
}
if(a[0][i]<j){
b[0][i]+=10-j;
}else{
b[0][i]-=j;
}
if(a[0][i+1]<j){
b[0][i+1]+=10-j;
}else{
b[0][i+1]-=j;
}
}
}
printf("%lld",ans);
}
return 0;
}
/*
2
2 8 3 5 5
2 8 3 5 1
*/