#include <cstdio>
using namespace std;
int n, m, i, j, k, a, b;
int str[1050][1050], vis[1050], top[1050];
int main() {
	scanf("%d", &n);
	for (i = 1; i <= n; i++) {
		for (j = 1; j < n; j++) {
			scanf("%d", &str[i][j]);
		}
	}
	for (i = 1; i <= n; i++) {
		top[i] = 1;
	}
	int day = 1;
	while (1) {
		bool con = false;
		for (i = 1; i <= n; i++) {
			if (vis[i] == day||top[i]>=n)
				continue;
			int v = str[i][top[i]];
			if (vis[v] != day && str[v][top[v]] == i) {
				top[v]++; top[i]++;
				vis[v] = day; vis[i] = day;
				con = true;
			}
		}
			if (!con)
				break;
			day++;
	}
	for (i = 1; i <= n; i++) {
		if (top[i] != n)
			break;
	}
	if (i != n + 1)
		printf("-1\n");
	else
		printf("%d\n", day-1);
	return 0;
}