#include<iostream>
#include<queue>

using namespace std;

queue<int> q1, q2;

int d, cnt, N;
int nextarr[1010];
int p[1010];
int flag[1010];
int a[1010][1010];

int main(){
	cin >> N;
	for(int i = 1; i <= N; i++){
		for(int j = 1; j < N; j++){
			cin >> a[i][j];
		}
	}

	for(int i = 1; i <= N; i++){
		q1.push(i);
		p[i] = 1;
		nextarr[i] = a[i][1];
	}

	int amount = N * (N - 1);
	int temp;
	while(cnt != amount){
		d++;
		while(!q1.empty()){
			temp = q1.front();
			q1.pop();
			if(nextarr[nextarr[temp]] == temp && flag[temp] < d){
				q2.push(temp);
				q2.push(nextarr[temp]);
				flag[temp] = d;
				flag[nextarr[temp]] = d;
			}
		}

		if(q2.empty()) break;
		
		cnt += q2.size();
		while(!q2.empty()){
			temp = q2.front();
			q2.pop();
			p[temp]++;
			nextarr[temp] = a[temp][p[temp]];
			q1.push(temp);
		}
	}
	if(cnt == amount) cout << d;
	else cout << -1;
}