#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
long long A[N], inc[N], revinc[N], mix[N], revmix[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t, n, i, j;
  cin >> n;
  long long tot = 1e18 + 10;
  long long mx = 0;
  for (i = 1; i <= n; i++) cin >> A[i];
  for (i = 1; i <= n; i++) {
    inc[i] = inc[i - 1];
    if (A[i] > mx) {
      mx = A[i];
    } else {
      inc[i] += (mx + 1 - A[i]);
      mx++;
    }
    mix[i] = mx;
  }
  mx = 0;
  for (int i = n; i; i--) {
    revinc[i] = revinc[i + 1];
    if (A[i] > mx) {
      mx = A[i];
    } else {
      revinc[i] += (mx + 1 - A[i]);
      mx++;
    }
    revmix[i] = mx;
  }
  for (int i = 1; i <= n; i++) {
    tot = min(tot, inc[i - 1] + revinc[i + 1] + max(mix[i], revmix[i]) - A[i]);
  }
  cout << tot << "\n";
  return 0;
}
