#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> x(n);
  for (long long i = 0; i < n; i++) cin >> x[i];
  if (n < 3) {
    cout << 0;
    return;
  }
  vector<long long> inc(n), dec(n);
  vector<long long> maxinc(n), maxdec(n);
  maxinc[0] = x[0];
  for (long long i = 1; i < n; i++) {
    inc[i] = inc[i - 1] + max(0LL, maxinc[i - 1] + 1 - x[i]);
    maxinc[i] = max(x[i], maxinc[i - 1] + 1);
  }
  maxdec[n - 1] = x[n - 1];
  for (long long i = n - 2; i >= 0; i--) {
    dec[i] = dec[i + 1] + max(0LL, maxdec[i + 1] + 1 - x[i]);
    maxdec[i] = max(x[i], maxdec[i + 1] + 1);
  }
  long long ans = LLONG_MAX;
  for (long long i = 0; i < n; i++) {
    long long cur = (i > 0 ? inc[i - 1] : 0) + (i < n - 1 ? dec[i + 1] : 0) +
                    max(maxinc[i], maxdec[i]) - x[i];
    ans = min(ans, cur);
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
