#include <bits/stdc++.h>
using namespace std;
long long presum[1000016], sufsum[1000016];
int main() {
  long long n, i;
  cin >> n;
  long long a[n + 2], b[n + 2];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  for (i = 1; i < n; i++) {
    if (a[i] <= a[i - 1]) {
      presum[i] = presum[i - 1] + a[i - 1] + 1 - a[i];
      a[i] = 1 + a[i - 1];
    } else
      presum[i] = presum[i - 1];
  }
  for (i = n - 2; i >= 0; i--) {
    if (b[i] <= b[i + 1]) {
      sufsum[i] = sufsum[i + 1] + b[i + 1] + 1 - b[i];
      b[i] = 1 + b[i + 1];
    } else
      sufsum[i] = sufsum[i + 1];
  }
  long long ans = 9000000000000000000;
  for (i = 0; i < n; i++) {
    long long temp = 0;
    temp = presum[i] + sufsum[i + 1];
    if (a[i] == b[i + 1]) temp++;
    ans = min(ans, temp);
  }
  cout << ans << "\n";
}
