#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int N = 1e5 + 5;
long long pref[N], suf[N], b[N], c[N], a[N];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = max(a[i], b[i - 1] + 1);
    pref[i] = pref[i - 1] + (b[i] - a[i]);
  }
  for (int i = n; i > 0; i--) {
    c[i] = max(a[i], c[i + 1] + 1);
    suf[i] = suf[i + 1] + (c[i] - a[i]);
  }
  long long ans = inf;
  for (int i = 0; i <= n; i++) {
    ans = min(ans, pref[i] + suf[i + 1] + (b[i] == c[i + 1]));
  }
  cout << ans;
}
