#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define M (1000000007)
#define N (1010)
int n,ans,ti[N];
queue<int>a[N];

int main(){
	scanf("%d",&n); int x;
	for (int i=1;i<=n;++i)
		for (int j=1;j<n;++j){
			scanf("%d",&x);
			a[i].push(x);
		}
	bool f1,f2; int fir;
	while (1){
		f1=0,f2=0;
		for (int i=1;i<=n;++i){
			if (!a[i].empty()) f1=1;
			while (!a[i].empty()&&a[a[i].front()].front()==i){
				ti[i]=ti[a[i].front()]=max(ti[i],ti[a[i].front()])+1;
				a[a[i].front()].pop(),a[i].pop(),f2=1;
			}
		}
		if (!f1){
			for (int i=1;i<=n;++i) ans=max(ans,ti[i]);
			printf("%d",ans);
			return 0;
		}
		if (!f2){
			printf("-1\n");
			return 0;
		}
	}
}
