#include <bits/stdc++.h>
using namespace std;
int n;
int a[111111];
pair<int, long long> l[111111], r[111111];
void upd(long long &a, long long b) {
  if (a < 0 || a > b) a = b;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int last = 0;
  long long tot = 0LL;
  for (int i = 1; i <= n; i++) {
    if (a[i] <= last) tot += last + 1 - a[i];
    last = max(last + 1, a[i]);
    l[i] = make_pair(last, tot);
  }
  last = 0;
  tot = 0LL;
  for (int i = n; i > 0; i--) {
    if (a[i] <= last) tot += last + 1 - a[i];
    last = max(last + 1, a[i]);
    r[i] = make_pair(last, tot);
  }
  l[0] = r[n + 1] = make_pair(0, 0LL);
  long long ans = -1LL;
  for (int i = 0; i <= n; i++)
    upd(ans, l[i].second + r[i + 1].second +
                 ((l[i].first == r[i + 1].first) ? 1 : 0));
  cout << ans << endl;
  return 0;
}
