#include <bits/stdc++.h>
using namespace std;
long long n, bal[100010], jobb[100010], t[100010];
long long add;
int main() {
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%I64d", &t[i]);
    bal[i] = t[i];
    jobb[i] = t[i];
  }
  long long i = 1;
  while (t[i] < t[i + 1]) i++;
  for (long long ii = i; ii < n; ii++) {
    if (bal[ii] + 1 > bal[ii + 1]) bal[ii + 1] = bal[ii] + 1;
  }
  i = n;
  while (jobb[i - 1] > jobb[i]) i--;
  for (long long ii = i; ii > 1; ii--) {
    if (jobb[ii - 1] < jobb[ii] + 1) jobb[ii - 1] = jobb[ii] + 1;
  }
  for (i = 1; i <= n; i++) {
    if (min(jobb[i], bal[i]) > t[i]) {
      add += min(jobb[i], bal[i]) - t[i];
      t[i] = min(jobb[i], bal[i]);
    }
    if (t[i] == t[i - 1]) {
      add++;
      t[i]++;
    }
  }
  printf("%I64d\n", add);
  return 0;
}
