import Data.Array


--エッジ、現在位置、現在までのステップ数、出力
solver::(Array Int Int,Int)->Int->Int->Int
solver (tab,n) now step = if now == 2 then step
                                      else if step >= n then (-1)
                                                        else solver (tab,n) (tab!now) (step+1)
nget::Int -> IO [Int]
nget 0=return []
nget n= do
  c<-getLine
  rest<-nget (n-1)
  return ((read c):rest)

main::IO()
main=do
  nc<-getLine
  let n = read nc
  dat<-nget n
  let i_dat= listArray (1,n) dat
  print (solver (i_dat,n) 1 0)
