#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> arr(n), last_pref(n + 1, 0), last_suff(n, 0);
  vector<long long> pref(n + 1, 0), suff(n, 0);
  for (int i = 0; i < n; i++) cin >> arr[i];
  last_pref[0] = arr[0];
  for (int i = 1; i < n; i++) {
    if (arr[i] <= last_pref[i - 1]) {
      last_pref[i] = last_pref[i - 1] + 1;
      pref[i] = pref[i - 1] + last_pref[i - 1] - arr[i] + 1;
    } else {
      last_pref[i] = arr[i];
      pref[i] = pref[i - 1];
    }
  }
  last_suff[n - 1] = arr[n - 1];
  for (int i = n - 2; i >= 0; --i) {
    if (arr[i] <= last_suff[i + 1]) {
      last_suff[i] = last_suff[i + 1] + 1;
      suff[i] = suff[i + 1] + last_suff[i + 1] - arr[i] + 1;
    } else {
      last_suff[i] = arr[i];
      suff[i] = suff[i + 1];
    }
  }
  long long ans = 2e18;
  for (int i = 0; i < n - 1; i++) {
    ans = min(ans, pref[i] + suff[i + 1] + (last_pref[i] == last_suff[i + 1]));
  }
  ans = min(ans, pref[n - 1]);
  cout << ans << endl;
  return 0;
}
