#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
#define REP(i, n) for(int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

int main() {
	int n;
	cin >> n;
	vector<queue<int>> q(n + 1);
	int a;
	REP(i, n) {
		REP(j, n - 1) {
			cin >> a;
			q[i + 1].push(a);
		}
	}
	VI t(n + 1, 0);
	int i, flaga, flagb;
	for (i = 0; 1; i++) {
		flaga = 0; flagb = 0;
		for(int j = n;j >= 1;j--) {
			if (!q[j].empty()) {
				flaga = 1;
				if (t[j] != 1 && t[q[j].front()] != 1) {
					if (q[q[j].front()].front() == j) {
						//cout << j << ' ' << q[j].front() << ' ' << i << '\n';
						flagb = 1;
						t[j] = 1;
						t[q[j].front()] = 1;
						q[q[j].front()].pop();
						q[j].pop();
					}
				}
			}
		}
		REP(i, n)
			t[i + 1] = 0;
		if (flaga == 0)
			break;
		else if (flagb == 0) {
			i = -1;
			break;
		}
	}
	cout << i;
}