import Control.Monad (replicateM)

f :: [Integer] -> Integer -> Maybe Integer
f (a:as) i
  | a > i     = Nothing
  | otherwise = case as of
                  []     -> Just 0
                  (b:as) -> case compare (a + 1) b of
                              LT -> Nothing
                              EQ -> fmap (+ 1) (f (b:as) (i + 1))
                              GT -> fmap (+ b) (f (b:as) (i + 1))

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