#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> in(n);
  for (auto& i : in) scanf("%d", &i);
  vector<int> tmp = in;
  int i1 = 0;
  long long ans = numeric_limits<long long>::max();
  long long cur = 0;
  for (int i = n - 2; i >= 0; i--) {
    int newV = max(in[i], tmp[i + 1] + 1);
    cur += (newV - tmp[i]);
    tmp[i] = newV;
  }
  ans = cur;
  cur -= (tmp[0] - in[0]);
  tmp[0] = in[0];
  ans = min(ans, cur);
  for (int i = 1; i < n; i++) {
    int newV = max(tmp[i - 1] + 1, in[i]);
    if (i + 1 < n) newV = max(newV, tmp[i + 1] + 1);
    cur -= (tmp[i] - newV);
    ans = min(ans, cur);
    cur -= tmp[i] - max(tmp[i - 1] + 1, in[i]);
    tmp[i] = max(tmp[i - 1] + 1, in[i]);
  }
  printf("%I64d\n", ans);
  return 0;
}
