import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; 
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return read.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtype!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint;
	
	int[][] af;
	foreach(i; 0 .. n) af ~= rint(n - 1).map!(x => x - 1).array;
	log("af:", af);
	
	int[] js = new int[](n);
	S[] q;
	int qx = 0;
	foreach(i; 0 .. n){
		int x = af[i][js[i]];
		if(i < x && af[x][js[x]] == i) q ~= S(i, x);
	}
	int ans = 0;
	while(1){
		log("q:", q, "qx:", qx);
		if(qx >= q.length){
			bool f = 0;
			foreach(i; 0 .. n) if(js[i] < n - 1) f = 1;
			if(f){
				"-1".writeln;
				return;
			}
			else break;
		}
		int qx0 = q.length.to!int;
		while(qx < qx0){
			S p = q[qx];
			js[p.x] += 1, js[p.y] += 1;
			if(js[p.x] < n - 1){
				int x2 = af[p.x][js[p.x]];
				if(af[x2][js[x2]] == p.x) q ~= S(p.x, x2);
			}
			if(js[p.y] < n - 1){
				int y2 = af[p.y][js[p.y]];
				if(af[y2][js[y2]] == p.y) q ~= S(p.y, y2);
			}
			qx += 1;
		}
		ans += 1;
	}
	
	ans.writeln;
}

struct S{
	int x, y;
}
