import Control.Monad

main = do
    n <- read <$> getLine
    as <- replicateM n (read <$> getLine)
    let result = f as 1 (Just 0)
    printResult result

printResult :: Maybe Int -> IO ()
printResult (Just count) = print count
printResult _ = print (-1)

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