#include <bits/stdc++.h>
using namespace std;
long long a[100005], n, b[100005], c[100005], f[100005], s[100005];
int main() {
  ios_base::sync_with_stdio(false);
  long long precision = numeric_limits<double>::digits10;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  b[1] = a[1];
  for (long long i = 2; i <= n; i++) {
    b[i] = max(a[i], b[i - 1] + 1);
    f[i] = f[i - 1] + b[i] - a[i];
  }
  c[n] = a[n];
  for (long long i = n - 1; i >= 1; i--) {
    c[i] = max(a[i], c[i + 1] + 1);
    s[i] = s[i + 1] + c[i] - a[i];
  }
  if (n == 1)
    cout << 0 << endl;
  else {
    long long ans = min(f[n], s[1]);
    if (n >= 3) {
      for (long long i = 2; i <= n - 1; i++) {
        long long inc = min(b[i] - a[i], c[i] - a[i]);
        ans = min(ans, f[i] + s[i] - inc);
      }
    }
    cout << ans << endl;
  }
}
