#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fl;
const long double pi = acos(-1);
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
long long v[N];
long long pd1[N], pd2[N];
long long mn1[N], mn2[N];
int n;
void solve1() {
  for (int i = 1; i <= n; i++) {
    pd1[i] = pd1[i - 1];
    mn1[i] = v[i];
    if (v[i] <= mn1[i - 1]) {
      mn1[i] = mn1[i - 1] + 1;
      pd1[i] += (long long)(mn1[i] - v[i]);
    }
  }
}
void solve2() {
  for (int i = n; i >= 1; i--) {
    pd2[i] = pd2[i + 1];
    mn2[i] = v[i];
    if (v[i] <= mn2[i + 1]) {
      mn2[i] = mn2[i + 1] + 1;
      pd2[i] += (long long)(mn2[i] - v[i]);
    }
  }
}
long long vai() {
  long long r = LINF;
  for (int i = 1; i <= n; i++) {
    long long x = pd1[i - 1] + pd2[i + 1];
    long long preciso = max(mn1[i - 1], mn2[i + 1]) + 1;
    x += max(0ll, preciso - v[i]);
    r = min(r, x);
  }
  return r;
}
int main() {
  ios::sync_with_stdio(false);
  ;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> v[i];
  solve1();
  solve2();
  cout << vai() << endl;
  return 0;
}
