#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n;
long long a[MAXN], sum1[MAXN], sum2[MAXN];
long long l[MAXN], r[MAXN];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  a[0] = a[n + 1] = 0;
  l[1] = a[1];
  r[n] = a[n];
  sum1[0] = 0;
  sum1[1] = 0;
  sum2[n] = 0;
  sum2[n + 1] = 0;
  for (int i = 2; i <= n; ++i) {
    sum1[i] = sum1[i - 1];
    if (a[i] <= l[i - 1]) {
      sum1[i] += (l[i - 1] + 1 - a[i]);
      l[i] = l[i - 1] + 1;
    } else {
      l[i] = a[i];
    }
  }
  for (int i = n - 1; i >= 1; --i) {
    sum2[i] = sum2[i + 1];
    if (a[i] <= r[i + 1]) {
      sum2[i] += (r[i + 1] + 1 - a[i]);
      r[i] = r[i + 1] + 1;
    } else {
      r[i] = a[i];
    }
  }
  long long ans = 1e18;
  for (int i = 0; i <= n; ++i) {
    if (l[i] != r[i + 1])
      ans = min(ans, sum1[i] + sum2[i + 1]);
    else
      ans = min(ans, sum1[i] + sum2[i + 1] + 1);
  }
  cout << ans << endl;
  return 0;
}
