process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
    const N = lines[0]-0;
    const map = [];
    for(let i=1;i<=N;i++){
        map[i] = lines[i];
    }
    const chk = {};
    let p = 1;
    let ans = 0;
    while(p!=2){
        chk[p] = true;
        p = map[p];
        if(chk[p]){
            ans = -1;
            break;
        }
        ans++;
    }
    console.log(ans);
});

min = (...a)=>{return a.reduce((a,b)=>a<b?a:b, a[0])};
