import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;
import std.regex;
import std.range;
import core.exception;

void main() {
	auto N = readln.chomp.to!int;
	auto a = new int[](N);
	foreach(i; 0..N) {
		a[i] = readln.chomp.to!int;
	}
	bool[int] b;
	int current = 1, count;
	while(true) {
		try {
			if(b[current]) {
				(-1).writeln;
				return;
			}
		} catch(RangeError e) { }
		if(a[current-1] == 2) {
			(count+1).writeln;
			return;
		}
		b[current] = true;
		current = a[current-1];
		count++;
	}
}
