#!/usr/local/gnu/bin/bash
read n
a=()
passed=(`printf %$n's' | tr ' ' '0' | fold -1`)
for i in `seq $n`
do
    read x
    a[$i]=$x
done
i=1
cnt=0
while((i!=2&&passed[i]==0))
do
    passed[$i]=1
    i=${a[i]}
    cnt=$((cnt+1))
done
((i==2)) && echo $cnt || echo -1
