#include<bits/stdc++.h>
using namespace std;
struct locks
{
int x1,x2,x3,x4,x5;
}cnt[1000];
int a[9][6],n,c,ccnt;
bool cmp(locks a,locks b)
{
if(a.x1 != b.x1) return a.x1 < b.x1;
if(a.x2 != b.x2) return a.x2 < b.x2;
if(a.x3 != b.x3) return a.x3 < b.x3;
if(a.x4 != b.x4) return a.x4 < b.x4;
return a.x5 < b.x5;
}
signed main()
{
cin>>n;
for(int k = 1;k <= n;k++)
for(int i = 1;i <= 5;i++) cin>>a[k][i];
if(n == 1)
{
cout<<81;
return 0;
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= 9;j++)
{
cnt[++c].x1 = (a[i][1] + j) % 10,cnt[c].x2 = a[i][2],cnt[c].x3 = a[i][3],cnt[c].x4 = a[i][4],cnt[c].x5 = a[i][5];
cnt[++c].x1 = a[i][2],cnt[c].x2 = (a[i][2] + j) % 10,cnt[c].x3 = a[i][3],cnt[c].x4 = a[i][4],cnt[c].x5 = a[i][5];
cnt[++c].x1 = a[i][1],cnt[c].x2 = a[i][2],cnt[c].x3 = (a[i][3] + j) % 10,cnt[c].x4 = a[i][4],cnt[c].x5 = a[i][5];
cnt[++c].x1 = a[i][1],cnt[c].x2 = a[i][2],cnt[c].x3 = a[i][3],cnt[c].x4 = (a[i][4] + j) % 10,cnt[c].x5 = a[i][5];
cnt[++c].x1 = a[i][1],cnt[c].x2 = a[i][2],cnt[c].x3 = a[i][3],cnt[c].x4 = a[i][4],cnt[c].x5 = (a[i][5] + j) % 10;
}
for(int j = 1;j <= 9;j++)
{
cnt[++c].x1 = (a[i][1] + j) % 10,cnt[c].x2 = (a[i][2] + j) % 10,cnt[c].x3 = a[i][3],cnt[c].x4 = a[i][4],cnt[c].x5 = a[i][5];
cnt[++c].x1 = a[i][1],cnt[c].x2 = (a[i][2] + j) % 10,cnt[c].x3 = (a[i][3] + j) % 10,cnt[c].x4 = a[i][4],cnt[c].x5 = a[i][5];
cnt[++c].x1 = a[i][1],cnt[c].x2 = a[i][2],cnt[c].x3 = (a[i][3] + j) % 10,cnt[c].x4 = (a[i][4] + j) % 10,cnt[c].x5 = a[i][5];
cnt[++c].x1 = a[i][1],cnt[c].x2 = a[i][2],cnt[c].x3 = a[i][3],cnt[c].x4 = (a[i][4] + j) % 10,cnt[c].x5 = (a[i][5] + j) % 10;
}
}
sort(cnt + 1,cnt + c + 1,cmp);
int tmp = 0;
for(int i = 1;i <= c;i++)
{
if(cnt[i].x1 == cnt[i - 1].x1 && cnt[i].x2 == cnt[i - 1].x2 && cnt[i].x3 == cnt[i - 1].x3 && cnt[i].x4 == cnt[i - 1].x4 && cnt[i].x5 == cnt[i - 1].x5)
tmp++;
else tmp = 1;
if(tmp == n) ccnt++;
}
cout<<ccnt;
return 0;
}