#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#define P 10000000019LL
#define P2 10000001707LL
#define N 111
#define mmt(a) memset(a,0,sizeof(a))
#define FOR(i,a,b) for(i=(a);i<=(b);i++)
#define ROF(i,a,b) for(i=(a);i>=(b);i--)
typedef unsigned long long LL;
using namespace std;
LL Pow[N];
int a[N][N],b[N][N];
LL pb0[N][N][N],pb1[N][N][N],pa0[N][N][N],pa1[N][N][N];
LL ha[N][N][N],hb[N][N][N],sa[N][N*N],sb[N][N*N],la[N],lb[N];
int n;
int main()
{
scanf("%d",&n);
Pow[1]=1;int i,j,k;
FOR(i,2,50) Pow[i]=Pow[i-1]*P;
FOR(i,1,n) FOR(j,1,n) scanf("%d",&a[i][j]);
FOR(i,1,n) FOR(j,1,n) scanf("%d",&b[i][j]);
FOR(i,1,n)
{
FOR(j,1,n)
{
pb0[i][j][j]=b[i][j];pa0[i][j][j]=a[i][j];
FOR(k,j+1,n) pb0[i][j][k]=pb0[i][j][k-1]*P+b[i][k];
FOR(k,j+1,n) pa0[i][j][k]=pa0[i][j][k-1]*P+a[i][k];
}
}
FOR(j,1,n)
{
FOR(i,1,n)
{
pb1[j][i][i]=b[i][j];pa1[j][i][i]=a[i][j];
FOR(k,i+1,n) pb1[j][i][k]=pb1[j][i][k-1]*P+b[k][j];
FOR(k,i+1,n) pa1[j][i][k]=pa1[j][i][k-1]*P+a[k][j];
}
}
mmt(la);mmt(lb);
FOR(i,1,n)
FOR(j,1,n)
{
LL tmp=P2;
ha[i][j][1]=a[i][j]*tmp;sa[1][++la[1]]=ha[i][j][1];
FOR(k,2,min(n-i+1,n-j+1))
{
tmp*=tmp;
ha[i][j][k]=ha[i][j][k-1]*tmp+pa0[i+k-1][j][j+k-1]+pa1[j+k-1][i][i+k-1];
sa[k][++la[k]]=ha[i][j][k];
}
}
FOR(i,1,n)
FOR(j,1,n)
{
LL tmp=P2;
hb[i][j][1]=b[i][j]*tmp;sb[1][++lb[1]]=hb[i][j][1];
FOR(k,2,min(n-i+1,n-j+1))
{
tmp*=tmp;
hb[i][j][k]=hb[i][j][k-1]*tmp+pb0[i+k-1][j][j+k-1]+pb1[j+k-1][i][i+k-1];
sb[k][++lb[k]]=hb[i][j][k];
}
}
int ans=0;
FOR(k,1,n) {sort(sa[k]+1,sa[k]+la[k]+1);sort(sb[k]+1,sb[k]+lb[k]+1);}
ROF(i,n,1)
{
bool f=0;
FOR(j,1,la[i])
{
LL t=sa[i][j];
if (binary_search(sb[i]+1,sb[i]+1+lb[i],t)) {f=1;break;}
}
if (f) {ans=i;break;}
}
printf("%d\n",ans);
}