import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;
import std.bigint;


void main()
{
  int n;
  readf("%s\n", &n);
  
  auto a = new int[](n);
  foreach (i; 0..n) {
    a[i] = readln.chomp.to!int;
  }
  int cnt, i;
  while (true) {
    if (a[i] == -1) {
      writeln("-1");
      break;
    } 
    cnt++;
    if (a[i] == 2) {
      writeln(cnt);
      break;
    }
    auto tmp = i;
    i = a[i] - 1;
    a[tmp] = -1;
  }
}
