module Main (main) where

import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as C8
import Data.Bool
import Data.Maybe
import qualified Data.Array as Array

main :: IO ()
main = do
  [n] <- readInts
  xs <- replicateM n readInts
  print $ solve n $ concat xs
  return ()

-- solve :: Int -> [Int] -> Output
solve n xs = g $ until p f (1, head xs, arr)
  where
    arr = Array.listArray (1,n) xs Array.// [(1,-1)]
    p (cnt, n, arr) = n == -1 || n == 2
    f (cnt, n, arr)
      | n ==  0   = (cnt, 0, arr)
      | n == -1   = (-1, -1, arr)
      | n ==  2   = (cnt, 0, arr)
      | otherwise = (cnt+1, arr Array.! n, arr Array.// [(n,-1)])
    g (a,b,c)
      | b == -1 = -1
      | otherwise = a

-- utils
readInts :: IO [Int]
readInts = map (fst . fromJust . C8.readInt) . C8.words <$> C8.getLine