#include <bits/stdc++.h>
using namespace std;
 
const int N = 2e5 + 5;
 
int A[N] , n;
 
int main(void) {
	scanf("%d" , &n);
	for(int i = 1;i <= n;++ i) {
		scanf("%d" , &A[i]);
		if(A[i] > A[i - 1] + 1) return 0 * puts("-1");
	}
	if(A[1]) return 0 * puts("-1");
	long long ans = 0;
	int now = n - A[n]; ans += A[n];
	for(int i = n - 1;i >= 1;-- i) 
		if(i - A[i] != now) now = i - A[i] , ans += A[i];
	cout << ans << endl;
}