#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long a[N];
int n;
long long pre[N], suff[N], as[N], ap[N];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
  }
  long long cur = a[0];
  ap[0] = cur;
  for (int i = 1; i < n; i++) {
    cur = max(a[i], cur + 1);
    ap[i] = cur;
    pre[i] = pre[i - 1] + cur - a[i];
  }
  as[n - 1] = a[n - 1];
  cur = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    cur = max(a[i], cur + 1);
    as[i] = cur;
    suff[i] = suff[i + 1] + cur - a[i];
  }
  long long ans = 1LL << 60;
  for (int i = 0; i < n; i++) {
    long long left = (i > 0) ? pre[i - 1] : 0;
    long long right = suff[i];
    long long cost = left + right;
    if (i > 0 && ap[i - 1] == as[i]) {
      cost++;
    }
    ans = min(ans, cost);
    left = pre[i];
    right = (i < n - 1) ? suff[i + 1] : 0;
    cost = left + right;
    if (i < n - 1 && ap[i] == as[i + 1]) {
      cost++;
    }
    ans = min(ans, cost);
  }
  cout << ans << '\n';
  return 0;
}
