#include <bits/stdc++.h>

#define LL int

using namespace std;

LL a[1000][1000];

int main(){
	ios_base::sync_with_stdio(false);
	LL n;
	cin >> n;
	for (LL i = 0; i < n; i++)
		for (LL j = 0; j < n-1; j++){
			cin >> a[i][j];
			a[i][j]--;
		}
	bool pos = true;
	vector<LL> last(n,0);
	vector<LL> index(n,0);
	for (LL d = 0; d < n*(n-1)/2; d++){
		bool some = false;
		LL games = 0;
		for (LL i = 0; i < n; i++){
			if (index[i] < n-1)
				some = true;
			else
				continue;
			LL opp = a[i][index[i]];	
			if (index[opp] < n-1 && a[opp][index[opp]] == i){
				games++;
				LL day = max(last[i],last[opp])+1;
				last[i] = day;
				last[opp] = day;
				index[i]++;
				index[opp]++;
			}
		}
		if (!some){
			break;
		}
		if (!games){
			pos = false;
			break;
		}
	}
	if (!pos)
		cout << "-1\n";
	else
		cout << *(max_element(last.begin(),last.end())) << "\n";
}
