import Control.Monad (replicateM)

f :: [Int] -> Int -> Maybe Int
f (a:as) i
  | a <= i    = case as of
                  []     -> Just 0
                  (b:as) -> if a + 1 == b
                              then fmap (+ 1) (f (b:as) (i + 1))
                              else fmap (+ b) (f (b:as) (i + 1))
  | otherwise = Nothing

main = do
  n <- readLn :: IO Int
  a <- map read <$> replicateM n getLine :: IO [Int]
  print $ case f a 0 of
            Just i  -> i
            Nothing -> -1