import std.stdio,
	   std.algorithm,
	   std.string,
	   std.conv,
	   std.typecons,
	   std.container,
	   std.range;

void main() {
	int n;
	scanf("%d\n", &n);
	int[][] mat = new int[][n];
	foreach(i; 0..n) {
		mat[i] = readln.chomp.split.map!(s => s.to!int - 1).array;
	}

	long day;
	auto use = new long[n];
	for(;;) {
		++day;
		bool play = false;
		bool allEmpty = true;
		foreach (i; 0..n) {
			auto x = i;
			allEmpty &= mat[x].empty;
			if (mat[x].empty) continue;
			auto y = mat[i][0];
			if (x == y || mat[y].empty || use[x] == day || use[y] == day || mat[x][0] != y || mat[y][0] != x) continue;
			use[x] = day;
			use[y] = day;
			mat[x].popFront;
			mat[y].popFront;
			play |= true;
		}
		if (allEmpty) {
			write(day-1);
			return;
		}
		if (!play) {
			write(-1);
			return;
		}
	}
}
