#include <bits/stdc++.h>
using namespace std;
int a[100005], pre[100005], suf[100005];
long long preval[100005], sufval[100005];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (a[i] > pre[i - 1]) {
      pre[i] = a[i];
      preval[i] = preval[i - 1];
    } else {
      pre[i] = pre[i - 1] + 1;
      preval[i] = preval[i - 1];
      preval[i] += (pre[i] - a[i]);
    }
  }
  for (int i = n; i >= 1; i--) {
    if (a[i] > suf[i + 1]) {
      suf[i] = a[i];
      sufval[i] = sufval[i + 1];
    } else {
      suf[i] = suf[i + 1] + 1;
      sufval[i] = sufval[i + 1];
      sufval[i] += (suf[i] - a[i]);
    }
  }
  long long ans = 1ll << 61;
  for (int i = 1; i <= n; i++) {
    if (pre[i] == suf[i + 1])
      ans = min(ans, preval[i] + sufval[i + 1] + 1);
    else
      ans = min(ans, preval[i] + sufval[i + 1]);
  }
  cout << ans;
}
