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 . showAnswer $ 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

showAnswer = Number

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

data Answer
  = YesNo Bool
  | YESNO Bool
  | AB Bool
  | Number Int
  | String String
  | ByteString ByteString
  | Compare Ordering
  | Compare2 Ordering

instance Show Answer where
  show (YesNo b)       = bool "No" "Yes"  b
  show (YESNO b)       = bool "NO" "YES"  b
  show (AB b)          = bool "B"  "A"    b
  show (Number i)      = show i
  show (String s)      = s
  show (ByteString bs) = C8.unpack bs
  show (Compare o)     = ordering "<" "=" ">" o
  show (Compare2 o)    = ordering "Right" "Balanced" "Left" o

ordering :: a -> a -> a -> Ordering -> a
ordering lt eq gt o = case o of
  LT -> lt
  EQ -> eq
  GT -> gt

-- | k 番目の文字に関数 f を適用する
indexMapBS :: Int -> (Char -> Char) -> ByteString -> ByteString
indexMapBS k f s = C8.concat [s1, C8.singleton (f c), s3]
  where
    (s1, s2) = C8.splitAt (k-1) s
    c = C8.head s2
    s3 = C8.tail s2

tuple31 (a,_,_) = a
tuple32 (_,b,_) = b
tupel33 (_,_,c) = c