void main() {
    int n = readln.chomp.to!int;
    int[] a = new int[n];
    foreach (i; 0 .. n) {
        a[i] = readln.chomp.to!int;
    }
    bool[] light = new bool[n];
    light[0] = true;
    int now = a[0];
    int cnt;
    while (!light[1]) {
        if (light[now-1]) {
            cnt = -1;
            break;
        }
        light[now-1] = true;
        now = a[now-1];
        ++cnt;
    }
    cnt.writeln;
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import std.ascii;
import std.uni;