function Main(inp) {
    var input = inp.split('\n').map(function(n) {
        return Number(n);
    });
    var N = input[0];
    var visited = new Array(N);
    var current = 1;
    var step = 1;
    var next = input[current];
    visited[current] = true;
    while (next !== 2) {
        current = next;
        next = input[current];
        if (visited[current]) {
            console.log(-1);
            return
        }
        visited[current] = true;
        step += 1;
    }
    console.log(step);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));
