import Data.Array
import qualified Data.IntSet as S

main = do
  li <- getLine
  let n = read li
  co <- getContents
  let as = map read $ lines co
  let ans = compute n as
  print ans

compute :: Int -> [Int] -> Int
compute n as = loop S.empty 1 0
  where
    aa = listArray (1,n) as
    loop s i c
      | S.member i s = -1
      | i == 2 = c
      | True = loop (S.insert i s) (aa ! i) (succ c)
