#include <bits/stdc++.h>
using namespace std;
int n, m, k;
pair<long long, long long> dp1[100005], dp2[100005];
long long x[100005];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> x[i];
  long long mx1 = x[0], mx2 = x[n - 1];
  dp1[0] = make_pair(0, x[0]);
  dp2[n - 1] = make_pair(0, x[n - 1]);
  dp1[-1] = make_pair(0, 1e18);
  dp2[n] = make_pair(0, 1e18);
  for (int i = 1; i < n; i++) {
    dp1[i] = make_pair(dp1[i - 1].first, x[i]);
    if (mx1 >= x[i]) {
      mx1++;
      dp1[i] = make_pair(dp1[i - 1].first + mx1 - x[i], mx1);
    }
    mx1 = max(mx1, x[i]);
  }
  for (int i = n - 2; i >= 0; i--) {
    dp2[i] = make_pair(dp2[i + 1].first, x[i]);
    if (mx2 >= x[i]) {
      mx2++;
      dp2[i] = make_pair(dp2[i + 1].first + mx2 - x[i], mx2);
    }
    mx2 = max(mx2, x[i]);
  }
  long long ans = 1e18;
  for (int i = 0; i < n; i++) {
    long long cnt = 0;
    if (dp1[i].second != dp2[i + 1].second) {
      cnt = dp1[i].first + dp2[i + 1].first;
    } else {
      cnt = dp1[i].first + dp2[i + 1].first + 1;
    }
    ans = min(ans, cnt);
  }
  cout << endl;
  cout << ans;
  return 0;
}
