import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

int calc(int[] a) {
    int p = 1;
    int step = 0;
    auto used = new int[a.length + 1];
    while (p != 2) {
        if (used[p]++) return -1;

        p = a[p];
        step++;
    }
    return step;
}

void main() {
    int n = readint;

    auto a = new int[n + 1];
    for (int i = 1; i <= n; i++) {
        a[i] = readint;
    }
    writeln(calc(a));
}
