#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 100000;
long long a[kMaxN];
long long pre[kMaxN], suf[kMaxN];
long long opt_prefix[kMaxN], opt_suffix[kMaxN];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  pre[0] = a[0];
  for (int i = 1; i < n; ++i) {
    opt_prefix[i] = opt_prefix[i - 1];
    if (a[i] <= pre[i - 1]) {
      opt_prefix[i] += pre[i - 1] - a[i] + 1;
      pre[i] = 1 + pre[i - 1];
    } else {
      pre[i] = a[i];
    }
  }
  suf[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; --i) {
    opt_suffix[i] = opt_suffix[i + 1];
    if (a[i] <= suf[i + 1]) {
      opt_suffix[i] += suf[i + 1] - a[i] + 1;
      suf[i] = 1 + suf[i + 1];
    } else {
      suf[i] = a[i];
    }
  }
  long long answer = min(opt_prefix[n - 1], opt_suffix[0]);
  for (int i = 1; i < n - 1; ++i) {
    long long opt = opt_prefix[i - 1] + opt_suffix[i + 1];
    long long el = max(pre[i - 1], suf[i + 1]) + 1;
    if (a[i] < el) {
      opt += el - a[i];
    }
    if (opt < answer) {
      answer = opt;
    }
  }
  cout << answer << endl;
}
