#include <bits/stdc++.h>
using namespace std;

int main() {
	long long ans = 0;
	int n;
	scanf("%d", &n);
	vector<int> v(n);
	for (int &x : v)
		scanf("%d", &x);

	bool flag = true;
	if (v[0] != 0)
		flag = false;

	for (int i = 1; i < n; i++) {
		if (v[i] > v[i - 1]) {
			ans++;
			if (v[i] > v[i - 1] + 1)
				flag = false;
		} else {
			ans += v[i];
		}
	}

	if (!flag)
		ans = -1;
	cout << ans << endl;
	return 0;
}
