#include <bits/stdc++.h>
using namespace std;
int n, a[100010], b[100010], c[100010];
long long f[100010], g[100010];
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]);
    f[i] = f[i - 1] + b[i] - a[i];
  }
  for (int i = n; i; i--) {
    c[i] = max(c[i + 1] + 1, a[i]);
    g[i] = g[i + 1] + c[i] - a[i];
  }
  long long ans = 1e18;
  for (int i = 1; i <= n; i++)
    ans = min(ans, f[i] + g[i] - (min(b[i], c[i]) - a[i]));
  cout << ans << endl;
  return 0;
}
