unittest
{
	assert( [ "3", "2 3", "1 3", "1 2" ].parse.expand.solve == 3 );
	assert( [ "4", "2 3 4", "1 3 4", "4 1 2", "3 1 2" ].parse.expand.solve == 4 );
	assert( [ "3", "2 3", "3 1", "1 2" ].parse.expand.solve == -1 );
}



import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = input.front.to!( long );
	auto ass = new long[][]( n, n - 1 );
	foreach( ref as; ass )
	{
		input.popFront;
		as = input.front.split.to!( long[] );
		as[] -= 1;
	}
	return tuple( ass );
}

auto solve( long[][] ass )
{
	auto ans = 0L;
	auto cs = new long[ ass.length ];
	auto ds = new long[ ass.length ];
	for( auto ct = 0L; ct < ass.length * ass[ 0 ].length; /* nop */ )
	{
		auto cn = 0L;
		ans++;
		
		for( auto i = 0L; i < cs.length; i++ )
		{
			if( ds[ i ] == ans || ass[ i ].length <= cs[ i ] ) continue;
			
			auto v = ass[ i ][ cs[ i ] ];
			if( ds[ v ] == ans || ass[ v ].length <= cs[ v ] || v < i ) continue;
			
			auto vv = ass[ v ][ cs[ v ] ];
			if( vv != i ) continue;
			
			cn += 2;
			cs[ i ]++;
			cs[ v ]++;
			ds[ i ] = ans;
			ds[ v ] = ans;
		}
		
		if( cn == 0 ) return -1;
		ct += cn;
	}
	return ans;
}
