#include<iostream>
#include<vector>
using namespace std;
int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  for(int i = 0; i < n; i++) cin >> a[i];
  long long ans = 0;
  bool c = true;
  if(a[0] > 0) c = false;
  for(int i = 1; i < n; i++) if(a[i] - a[i-1] > 1) c = false;
  for(int i = 1; i < n; i++) 
    if(a[i] == a[i-1] + 1) ans++;
  	else ans += a[i];
  if(c) cout << ans << endl;
  else cout << -1 << endl;
}