#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
long long a[N], sl[N], sr[N], ml[N], mr[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= (n); i++) scanf("%lld", &a[i]);
  sl[1] = 0;
  ml[1] = a[1];
  for (int i = (2); i <= (n); i++) {
    ml[i] = max(ml[i - 1] + 1, a[i]);
    sl[i] = sl[i - 1] + ml[i] - a[i];
  }
  sr[n] = 0;
  mr[n] = a[n];
  for (int i = (n - 1); i >= (1); i--) {
    mr[i] = max(mr[i + 1] + 1, a[i]);
    sr[i] = sr[i + 1] + mr[i] - a[i];
  }
  long long ans = min(sr[1], sl[n]);
  for (int i = (2); i <= (n - 1); i++) {
    long long t = sl[i - 1] + sr[i + 1];
    long long p = max(max(ml[i - 1] + 1, mr[i + 1] + 1), a[i]) - a[i];
    ans = min(ans, t + p);
  }
  printf("%lld\n", ans);
  return 0;
}
