program ABC065B
    implicit none
    integer(8)::N,i,cn
    integer(8)::flg=0
    integer(8),allocatable,dimension(:)::A

    read(5,*)N
    allocate(A(N))
    read(5,*)(A(i),i=1,N)

    cn=1
    do i=1,N+1
        if(cn==2)then
            flg=1
            print'(i0)',i-1
            exit
        end if
        cn=A(cn)
    end do

    if(flg==0)then
        print'(i0)',-1
    end if
end program ABC065B