import Control.Monad
import Control.Monad.Primitive (PrimState)
import qualified Data.Vector.Unboxed.Mutable as MV
import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)

solve :: MV.MVector (PrimState IO) Int -> MV.MVector (PrimState IO) Bool -> Int -> Int -> IO (Int, Int)
solve mva mvb i count = do
    next <- MV.unsafeRead mva (i - 1)
    if next == 2
        then return (next, count)
        else do
            b <- MV.unsafeRead mvb (next - 1)
            if b
                then return (next, count)
                else do
                    MV.unsafeWrite mvb (next - 1) True
                    solve mva mvb next (count + 1)
main :: IO ()
main = do
    n <- fst . fromJust . B.readInt <$> B.getLine
    mva <- MV.replicate 100 0 :: IO (MV.MVector (PrimState IO) Int)
    mvb <- MV.replicate 100 False :: IO (MV.MVector (PrimState IO) Bool)
    forM_ [0..(n - 1)] $ \i -> do
        a <- fst . fromJust . B.readInt <$> B.getLine
        MV.write mva i a
    (next, count) <- solve mva mvb 1 1
    print $ if next == 2 then count else -1
