import Control.Monad

main = do
    n <- read <$> getLine
    as <- replicateM n (read <$> getLine)
    print $ f as 1 0

f :: [Int] -> Int -> Int -> Int
f _ 2 count = count
f list i count = if length list < count then -1 else f list next (count + 1)
    where next = list !! (i - 1)