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
    if next == 1
        then return (next, count)
        else do
            b <- MV.unsafeRead mvb next
            if b
                then return (next, count)
                else do
                    MV.unsafeWrite mvb next True
                    solve mva mvb next (count + 1)
main :: IO ()
main = do
    n <- fst . fromJust . B.readInt <$> B.getLine
    mva <- MV.replicate n 0 :: IO (MV.MVector (PrimState IO) Int)
    mvb <- MV.replicate n 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 - 1)
    (stop, count) <- solve mva mvb 0 0
    print $ if stop == 1 then count + 1 else -1
