#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e5 + 10;
int n, a[MAXN];
int b[MAXN], c[MAXN];
long long f[MAXN], g[MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) b[i] = max(b[i - 1] + 1, a[i]);
  for (int i = n; i >= 1; --i) c[i] = max(c[i + 1] + 1, a[i]);
  for (int i = 1; i <= n; ++i) f[i] = f[i - 1] + b[i] - a[i];
  for (int i = n; i >= 1; --i) g[i] = g[i + 1] + c[i] - a[i];
  long long ans = (long long)1e18;
  for (int i = 1; i <= n; ++i)
    ans = min(ans, f[i] + g[i] - (c[i] - a[i]) - (b[i] - a[i]) +
                       max(b[i], c[i]) - a[i]);
  cout << ans << endl;
  return 0;
}
