#include <bits/stdc++.h>
using namespace std;
long long int pref[100005], suff[100005], pref_moves[100005],
    suff_moves[100005], arr[100005], brr[100005], crr[100005];
int main() {
  ios::sync_with_stdio(false);
  ;
  cin.tie(0);
  ;
  long long int i, j, n, x, y, ans, z;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> arr[i];
  if (n == 1) {
    cout << "0";
    return 0;
  }
  pref[1] = 0;
  brr[1] = arr[1];
  pref_moves[1] = 0;
  for (i = 2; i <= n; i++) {
    pref[i] = pref[i - 1];
    if (arr[i] > brr[i - 1]) {
      brr[i] = arr[i];
      pref_moves[i] = 0;
    } else {
      x = brr[i - 1] + 1;
      x = x - arr[i];
      pref[i] += x;
      brr[i] = brr[i - 1] + 1;
      pref_moves[i] = x;
    }
  }
  suff[n] = 0;
  crr[n] = arr[n];
  suff_moves[n] = 0;
  for (i = n - 1; i >= 1; i--) {
    suff[i] = suff[i + 1];
    if (crr[i + 1] < arr[i]) {
      crr[i] = arr[i];
      suff_moves[i] = 0;
    } else {
      x = crr[i + 1] + 1;
      x = x - arr[i];
      suff[i] += x;
      crr[i] = crr[i + 1] + 1;
      suff_moves[i] = x;
    }
  }
  ans = suff[1];
  for (i = 1; i <= n; i++) {
    x = brr[i];
    y = crr[i];
    if (x >= y) {
      z = pref[i] + suff[i];
      z = z - suff_moves[i];
      ans = min(ans, z);
    } else {
      z = pref[i] + suff[i];
      z = z - pref_moves[i];
      ans = min(ans, z);
    }
  }
  cout << ans;
  return 0;
}
