local n = io.read("*n")
local t = {}
for i = 1, n do
  t[i] = io.read("*n")
end

local function solve()
  if 0 < t[1] then return -1 end
  local cnt = 0
  for i = 2, n do
    if t[i - 1] + 1 < t[i] then
      return -1
    elseif t[i] < t[i - 1] + 1 then
      cnt = cnt + t[i - 1]
    end
  end
  return cnt + t[n]
end

print(solve())
