#include <bits/stdc++.h>
int n, a[110000], f[110000], g[110000];
long long te, ans, sf[110000], sg[110000];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  f[1] = a[1];
  for (int i = 2; i <= n; i++) {
    f[i] = f[i - 1] + 1;
    if (a[i] > f[i]) f[i] = a[i];
    sf[i] = sf[i - 1] + f[i] - a[i];
  }
  g[n] = a[n];
  for (int i = n - 1; i >= 1; i--) {
    g[i] = g[i + 1] + 1;
    if (a[i] > g[i]) g[i] = a[i];
    sg[i] = sg[i + 1] + g[i] - a[i];
  }
  ans = sg[1];
  if (sf[n] < ans) ans = sf[n];
  for (int i = 2; i <= n; i++) {
    te = sf[i - 1] + sg[i];
    if (f[i - 1] == g[i]) te++;
    if (te < ans) ans = te;
  }
  printf("%lld\n", ans);
  return 0;
}
