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



import std.conv;
import std.range;
import std.stdio;
import std.string;
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.strip.to!( long );
	auto as = new long[ n ];
	foreach( ref a; as )
	{
		input.popFront;
		a = input.front.strip.to!( long );
	}
	return tuple( as );
}

auto solve( long[] as )
{
	auto ps = new bool[ as.length ];
	auto i = 0L;
	auto c = 0L;
	
	while( i != 1 )
	{
		if( ps[ i ] ) return -1;
		ps[ i ] = true;
		i = as[ i ] - 1;
		c++;
	}
	
	return c;
}
