#!/usr/local/gnu/bin/bash
read n
a=()
for i in `seq $n`
do
    read t
    a[$i]=$t
done
cnt=0
i=1
while((i!=2&&a[i]!=-1))
do
    cnt=$((cnt+1))
    j=$i
    i=${a[i]}
    a[$j]=-1
done
((i==2)) && echo $cnt || echo -1
