#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> v(n);
  vector<long long> l(n);
  vector<long long> r(n);
  vector<long long> m(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  l[0] = 0;
  m[0] = 0;
  long long mx = v[0];
  for (int i = 1; i < n; i++) {
    mx = max(v[i], mx + 1);
    l[i] = l[i - 1] + mx - v[i];
    m[i] = l[i] - l[i - 1];
  }
  r[n - 1] = 0;
  m[n - 1] = 0;
  mx = v[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    mx = max(v[i], mx + 1);
    r[i] = r[i + 1] + mx - v[i];
    m[i] = max(m[i], r[i] - r[i + 1]);
  }
  long long ans = 9223372036854775807;
  int pos_ans;
  for (int i = 0; i < n; i++) {
    if (i > 0 && i < n - 1) {
      long long fake = ans;
      ans = min(ans, l[i - 1] + r[i + 1] + m[i]);
      if (fake != ans) pos_ans = i;
    }
  }
  ans = min(ans, l[n - 1]);
  ans = min(ans, r[0]);
  cout << ans << endl;
  return 0;
}
