#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-13;
const long double PI = acos(-1);
const int INF = (int)1e9;
const long long INFF = (long long)1e18;
const int mod = (int)1e9 + 7;
const int MXN = (int)2e5 + 7;
long long a[MXN];
long long ll[MXN], rr[MXN];
long long lv[MXN], rv[MXN];
int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n + 1; ++i) scanf("%lld", a + i);
  int pre = 0;
  for (int i = 1; i < n + 1; ++i) {
    ll[i] = ll[i - 1];
    if (a[i] <= pre) {
      ll[i] += pre - a[i] + 1;
      pre++;
    } else {
      pre = a[i];
    }
    lv[i] = pre;
  }
  pre = 0;
  for (int i = n; i >= 1; --i) {
    rr[i] = rr[i + 1];
    if (a[i] <= pre) {
      rr[i] += pre - a[i] + 1;
      pre++;
    } else {
      pre = a[i];
    }
    rv[i] = pre;
  }
  long long ans = INFF;
  ans = min(ans, ll[n]);
  ans = min(ans, rr[1]);
  for (int i = 1; i < n + 1; ++i) {
    if (lv[i] == rv[i + 1])
      ans = min(ans, ll[i] + rr[i + 1] + 1);
    else
      ans = min(ans, ll[i] + rr[i + 1]);
  }
  printf("%lld\n", ans);
  return 0;
}
