#include <bits/stdc++.h>

using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n - 1));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n - 1; ++j) {
			cin >> a[i][j];
			--a[i][j];
		}
	}
	vector<int> q[2];
	int cur = 0;
	for (int i = 0; i < n; ++i) {
		if (a[a[i][0]][0] == i) {
			q[cur].push_back(i);
		}
	}
	int ans = 0;
	vector<int> cnt(n);
	while (q[cur].size()) {
		++ans;
		q[cur ^ 1].clear();
		set<int> s;
		for (int x : q[cur]) {
			++cnt[x];
			if (cnt[x] < n - 1) {
				int y = a[x][cnt[x]];
				if (cnt[y] < n - 1 and a[y][cnt[y]] == x) {
					s.insert(x);
					s.insert(y);
				}
			}
		}
		for (int x : s) {
			q[cur ^ 1].push_back(x);
		}
		cur ^= 1;
	}
	for (int i = 0; i < n; ++i) {
		if (cnt[i] != n - 1) {
			ans = -1;
		}
	}
	cout << ans;

	return 0;
}