import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BC


main :: IO ()
main = do
    n <- read <$> getLine
    as <- replicateM n (fst . fromJust . BC.readInt <$> BC.getLine)
    putStrLn $
        maybe "-1"
              show
              (f [] as 1)
    where
        f :: [Int] -> [Int] -> Int -> Maybe Int
        f hs as i =
            if i `elem` hs then Nothing
            else case as !! (i - 1) of
                2 -> Just 1
                x -> (+ 1) <$> f (i:hs) as x
