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



import std.algorithm;
import std.container;
import std.conv;
import std.range;
import std.stdio;

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

auto solve( 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;
	}
	
	auto queue = DList!long( iota( 0L, n ) );
	auto us = new bool[ n ];
	us[] = true;
	auto ds = new long[ n ];
	auto t = n * ( n - 1 ) / 2;
	while( !queue.empty )
	{
		auto x = queue.front;
		queue.removeFront;
		us[ x ] = false;
		
		if( ass[ x ].empty ) continue;
		
		auto y = ass[ x ].front;
		if( ass[ y ].empty ) continue;
		
		auto z = ass[ y ].front;
		if( x != z ) continue;
		
		queue.insert( x );
		us[ x ] = true;
		
		if( !us[ y ] )
		{
			queue.insert( y );
			us[ y ] = true;
		}
		
		auto d = max( ds[ x ], ds[ y ] ) + 1;
		ds[ x ] = d;
		ds[ y ] = d;
		
		ass[ x ].popFront;
		ass[ y ].popFront;
		t--;
	}
	return ( 0 < t ) ? -1 : ds.reduce!max;
}
