#include <bits/stdc++.h>
using namespace std;
long long n, k, m, a[int(1e6 + 1)], b[int(1e6 + 1)], c[int(1e6 + 1)];
void Input() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i], b[i] = c[i] = a[i];
}
long long up[int(1e6 + 1)] = {}, down[int(1e6 + 1)];
void Solve() {
  for (int i = 2; i <= n; ++i) {
    up[i] = up[i - 1];
    if (b[i] <= b[i - 1]) up[i] += b[i - 1] - b[i] + 1, b[i] = b[i - 1] + 1;
  }
  for (int i = n - 1; i; --i) {
    down[i] = down[i + 1];
    if (c[i] <= c[i + 1]) down[i] += c[i + 1] - c[i] + 1, c[i] = c[i + 1] + 1;
  }
  long long res = 1e18;
  for (int i = 1; i <= n; ++i) {
    long long x;
    if (c[i] >= b[i]) x = (down[i] + up[i - 1]);
    if (c[i] <= b[i]) x = (up[i] + down[i + 1]);
    res = min(res, x);
  }
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Input();
  Solve();
}
