#include <bits/stdc++.h>
using namespace std;
long long a[100015];
long long l[2][100015];
long long r[2][100015];
long long n, le, ri;
long long da = LONG_LONG_MAX;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    l[0][i] = max(le + 1, a[i]) - a[i];
    l[1][i] = max(le + 1, a[i]);
    le = max(le + 1, a[i]);
  }
  for (int i = n; i > 0; i--) {
    r[0][i] = max(ri + 1, a[i]) - a[i];
    r[1][i] = max(ri + 1, a[i]);
    ri = max(ri + 1, a[i]);
  }
  for (int i = 1; i <= n; i++) l[0][i] += l[0][i - 1];
  for (int i = n; i >= 0; i--) r[0][i] += r[0][i + 1];
  for (int i = 0; i <= n; i++) {
    da = min(da, l[0][i] + r[0][i + 1]);
    if (l[1][i] == r[1][i + 1]) da++;
  }
  cout << da;
}
