import Control.Monad
import Data.Array

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

f :: Array Int Int -> Int -> Int -> Int -> Int
f list n i count = if i == 2 then count else if n < count then -1 else f list n next (count + 1)
    where next = list ! (i - 1)