import Control.Applicative

apply_maybe :: (a -> b) -> Maybe a -> Maybe b
apply_maybe _ Nothing = Nothing
apply_maybe f (Just a) = Just $ f a

sge' :: Int -> [Int] -> Maybe Int
sge' acc [_] = Just acc
sge' acc (a:(r@(b:_))) | b <= a     = sge' (acc + b) r
sge' acc (a:(r@(b:_))) | b == a + 1 = sge' (acc + 1) r
sge' _ _                            = Nothing

sge :: [Int] -> Int
sge l@(0:_) = case sge' 0 l of
    Nothing -> -1
    Just r  -> r
sge _ = -1

main :: IO ()
main = do
    n <- readLn
    a <- mapM (\_->readLn) [1..n]
    putStrLn $ show $ sge a
