#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
const int MX = 1000005;
int n, a[1005], pr[MX], s;
vector<int> ch[MX];
queue<int> qu[2];
int main(){
	scanf("%d", &n);
	rep(i,n) rep(j,n-1){
		scanf("%d", a+j); a[j]--;
		if(j){
			int p = max(i,a[j-1])*n + min(i,a[j-1]), c = max(i,a[j])*n + min(i,a[j]);
			ch[p].push_back(c);
			pr[c]++;
		}
	}
	rep(i,n*n) if(!pr[i]) qu[0].push(i);
	for(s = 0; 1; s++){
		if(qu[s&1].empty()) break;
		while(!qu[s&1].empty()){
			int t = qu[s&1].front(); qu[s&1].pop();
			for(int it: ch[t]) if((--pr[it]) == 0) qu[s&1^1].push(it);
		}
	}
	rep(i,n*n) if(pr[i]) return 0*puts("-1");
	printf("%d\n", s);
}