#include<bits/stdc++.h>
using namespace std;
#define M 1000005
vector<int>g[M];
int sz[M],w[M],sk[M],mx[M],id[1005][1005];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j)id[i][j]=(i-1)*n+j;
	for(int i=1;i<=n;++i){
		for(int j=1;j<n;++j)scanf("%d",&w[j]);
		for(int j=2;j<n;++j){
			int a=i,b=w[j],c=i,d=w[j-1];
			if(a>b)swap(a,b);
			if(c>d)swap(c,d);
			g[id[a][b]].push_back(id[c][d]);
			sz[id[c][d]]++;
		}
	}int l=1,r=0,ans=1;
	for(int i=1;i<=n;++i)
		for(int j=i+1;j<=n;++j)
			if(!sz[id[i][j]]){
				sk[++r]=id[i][j];
				mx[id[i][j]]=1;
			}
	while(l<=r){
		int p=sk[l++];
		ans=max(ans,mx[p]);
		for(int i=0;i<(int)g[p].size();++i){
			int v=g[p][i];
			mx[v]=max(mx[v],mx[p]+1);
			sz[v]--;if(sz[v]==0)sk[++r]=v;
		}
	}
	if(r!=n*(n-1)/2)puts("-1");
	else printf("%d",ans);
}