#include <bits/stdc++.h>
using namespace std;
const int N = 100002;
int n, a[N];
long long aL[N], aR[N], costL[N], costR[N];
int main() {
  scanf("%d", &n);
  for (int(i) = (1); (i) <= (int)(n); (i)++) scanf("%d", &a[i]);
  for (int(i) = (1); (i) <= (int)(n); (i)++) {
    aL[i] = a[i];
    if (aL[i] <= aL[i - 1]) {
      costL[i] = aL[i - 1] + 1 - aL[i];
      aL[i] = aL[i - 1] + 1;
    }
    costL[i] += costL[i - 1];
  }
  for (long long(i) = (n); (i) >= (1); --(i)) {
    aR[i] = a[i];
    if (aR[i] <= aR[i + 1]) {
      costR[i] = aR[i + 1] + 1 - aR[i];
      aR[i] = aR[i + 1] + 1;
    }
    costR[i] += costR[i + 1];
  }
  long long ans = 1000000000000000000ll;
  for (int(i) = (1); (i) <= (int)(n); (i)++) {
    long long curr = costL[i - 1] + costR[i + 1];
    long long bst = min(aL[i - 1], aR[i + 1]) + 1;
    if (bst == aL[i - 1] or bst == aR[i + 1]) ++bst;
    if (bst > a[i]) curr += bst - a[i];
    ans = min(ans, curr);
  }
  cout << ans;
}
