// tested by Hightail - https://github.com/dj3500/hightail
import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

int n;
int[] a;

void main() {
    scan(n);
    a = iota(n).map!(i => readln.chomp.to!int - 1).array;

    auto d = new int[](n);
    d[] = -1;
    d[0] = 0;
    int pos;

    while (pos != 1) {
        if (d[a[pos]] == -1) {
            d[a[pos]] = d[pos] + 1;
            pos = a[pos];
        }
        else {
            break;
        }
    }

    writeln(d[1]);
}


void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}
