#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
long long suml[N];
long long bestl[N];
long long sumr[N];
long long bestr[N];
long long val[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &val[i]);
  }
  suml[0] = 0;
  bestl[0] = val[0];
  for (int i = 1; i < n; i++) {
    bestl[i] = max(bestl[i - 1] + 1, val[i]);
    suml[i] = suml[i - 1] + (bestl[i] - val[i]);
  }
  sumr[n - 1] = 0;
  bestr[n - 1] = val[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    bestr[i] = max(bestr[i + 1] + 1, val[i]);
    sumr[i] = sumr[i + 1] + (bestr[i] - val[i]);
  }
  long long ans = min(suml[n - 1], sumr[0]);
  for (int i = 0; i < n - 1; i++) {
    if (bestl[i] != bestr[i + 1]) {
      ans = min(ans, suml[i] + sumr[i + 1]);
    } else if (bestl[i] == bestr[i + 1]) {
      ans = min(ans, suml[i] + sumr[i + 1] + 1);
    }
  }
  printf("%lld\n", ans);
}
