#include <iostream>
using namespace std;
typedef long long ll;
ll N;
ll A[200010];

int main(){
	cin >> N;
	bool judge = true;
	for(int i=0;i<N;i++){
		cin >> A[i];
		if(A[i]>i) judge = false;
		if(i>=1){
			if(A[i]-A[i-1]>=2) judge = false;
		}
	}
	if(!judge){
		cout << -1 << endl;
		return 0;
	}
	ll ans = 0;
	for(int i=1;i<N;i++){
		if(A[i]-A[i-1]==1) ans++;
		else ans += A[i];
	}
	cout << ans << endl;
}