import           Control.Monad
import           Data.Maybe                     ( fromJust )
import qualified Data.Vector.Unboxed           as V
import qualified Data.ByteString.Lazy.Char8    as BC

main = do
    n  <- readLn
    as <-
        V.fromList
        .   map (pred . fst . fromJust . BC.readInt)
        .   BC.lines
        <$> BC.getContents
    print $ solve as 0 0 n

solve as 1 i _ = i
solve as k i n | i == n    = -1
               | otherwise = solve as (as V.! k) (succ i) n
