#include <bits/stdc++.h>
using namespace std;
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, 1, 0, -1};
long long gcd(long long x, long long y) {
  if (y == 0)
    return x;
  else
    return gcd(y, x % y);
}
long long expo(long long n, long long m, long long p) {
  long long r = 1;
  n = n % p;
  while (m > 0) {
    if (m % 2) r = (r * n) % p;
    n = (n * n) % p;
    m = m / 2;
  }
  return r % p;
}
bool isPrime(long long n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, ans = 0;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n == 1) {
    cout << 0 << '\n';
    return 0;
  }
  vector<long long> b(n), c(n), d(n);
  b[0] = a[0];
  for (long long i = 1; i < n; i++) {
    if (a[i] >= b[i - 1] + 1) {
      b[i] = a[i];
      c[i] = 0;
    } else {
      c[i] = b[i - 1] + 1 - a[i];
      b[i] = b[i - 1] + 1;
    }
    ans = ans + c[i];
  }
  long long s = ans;
  d[n - 1] = a[n - 1];
  s -= c[n - 1];
  if (b[n - 2] == a[n - 1]) {
    ans = min(ans, s + 1);
  }
  for (long long i = n - 2; i >= 0; i--) {
    s -= c[i];
    if (a[i] > d[i + 1]) {
      d[i] = a[i];
      c[i] = 0;
    } else {
      c[i] = d[i + 1] + 1 - a[i];
      d[i] = d[i + 1] + 1;
    }
    s += c[i];
    if (i > 0) {
      if (b[i - 1] == d[i]) {
        ans = min(ans, s + 1);
      } else {
        ans = min(ans, s);
      }
    } else
      ans = min(ans, s);
  }
  cout << ans << '\n';
}
