#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long a[n], i, b[n], c[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
    c[i] = a[i];
  }
  long long inc[n];
  inc[0] = 0;
  for (i = 1; i < n; i++) {
    inc[i] = inc[i - 1];
    if (a[i] <= a[i - 1]) {
      inc[i] += a[i - 1] - a[i] + 1;
      a[i] = a[i - 1] + 1;
    }
  }
  long long dec[n];
  dec[n - 1] = 0;
  for (i = n - 2; i >= 0; i--) {
    dec[i] = dec[i + 1];
    if (b[i] <= b[i + 1]) {
      dec[i] += b[i + 1] - b[i] + 1;
      b[i] = b[i + 1] + 1;
    }
  }
  long long ans = 100000000000000000, temp;
  for (i = 1; i < n - 1; i++) {
    temp = inc[i - 1] + dec[i + 1];
    long long me = max(a[i - 1], b[i + 1]);
    if (c[i] <= me) {
      temp += me - c[i] + 1;
    }
    if (temp < ans) ans = temp;
  }
  if (dec[0] < ans) ans = dec[0];
  if (inc[n - 1] < ans) ans = inc[n - 1];
  cout << ans << endl;
  return 0;
}
