#include <bits/stdc++.h>
using namespace std;
using pi = pair<long long, long long>;
using vi = vector<long long>;
using vvi = vector<vector<long long> >;
const long long inf = 2e18;
const long long NUM = 1000030;
const long long N = 10000000;
vector<long long> lp, sieve;
vector<long long> pr;
void primefactor();
long long binpow(long long a, long long b);
long long binpow(long long a, long long b, long long mod);
long long gcd(long long a, long long b);
long long lcm(long long a, long long b);
bool comp(long long a, long long b);
long long inversemod(long long a, long long mod);
void calc_sieve();
void test() {
  long long n, i;
  cin >> n;
  vi arr(n), start(n), end(n);
  for (i = 0; i < n; i++) cin >> arr[i];
  start[0] = 0;
  end[n - 1] = 0;
  vi copy(n), copy1(n);
  for (i = 0; i < n; i++) {
    copy[i] = arr[i];
    copy1[i] = arr[i];
  }
  for (i = 1; i < n; i++) {
    start[i] = start[i - 1];
    if (copy[i] <= copy[i - 1]) {
      long long temp = copy[i - 1] - copy[i];
      start[i] += temp + 1;
      copy[i] += temp + 1;
    }
  }
  for (i = n - 2; i >= 0; i--) {
    end[i] = end[i + 1];
    if (copy1[i] <= copy1[i + 1]) {
      long long temp = copy1[i + 1] - copy1[i];
      end[i] += temp + 1;
      copy1[i] += temp + 1;
    }
  }
  long long ans = inf;
  for (i = 1; i < n - 1; i++) {
    ans = min(ans, end[i + 1] + start[i - 1] +
                       max(end[i] - end[i + 1], start[i] - start[i - 1]));
  }
  ans = min(ans, min(end[0], start[n - 1]));
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1;
  while (t--) test();
  return 0;
}
void calc_sieve() {
  sieve.resize(NUM + 1, 0);
  for (long long x = 2; x <= NUM; x++) {
    if (sieve[x]) continue;
    for (long long u = x; u <= NUM; u += x) {
      sieve[u] = x;
    }
  }
}
void primefactor() {
  lp.resize(N + 1, 0);
  for (long long i = 2; i <= N; ++i) {
    if (lp[i] == 0) {
      lp[i] = i;
      pr.push_back(i);
    }
    for (long long j = 0;
         j < (long long)pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j)
      lp[i * pr[j]] = pr[j];
  }
}
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
long long binpow(long long a, long long b, long long mod) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res % mod;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return ((a / gcd(a, b)) * b); }
bool comp(long long a, long long b) { return a > b; }
long long inversemod(long long a, long long mod) {
  return binpow(a, mod - 2, mod);
}
