#include <bits/stdc++.h>
const double eps = 1e-10;
const float epsf = 1e-6;
using namespace std;
inline long long int __gcd(long long int a, long long int b) {
  if (a == 0 || b == 0) {
    return max(a, b);
  }
  long long int tempa, tempb;
  while (1) {
    if (a % b == 0)
      return b;
    else {
      tempa = a;
      tempb = b;
      a = tempb;
      b = tempa % tempb;
    }
  }
}
inline int compfloat(float& x, float& y) {
  if (fabs(x - y) < epsf)
    return 0;
  else if (x - y > 0)
    return 1;
  return -1;
}
inline int compdouble(double x, double y) {
  if (fabs(x - y) < eps)
    return 0;
  else if (x - y > 0)
    return 1;
  else
    return -1;
}
bool prime(long long int k) {
  for (long long int i = 2; i * i <= k; i++)
    if (k % i == 0) {
      return false;
    }
  return true;
}
void pdash(int n = 1) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 30; j++) {
      cout << "-";
    }
    cout << "\n";
  }
}
void solve() {
  long long int n;
  cin >> n;
  long long int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  long long int dpi[n], dpd[n];
  dpi[0] = 0;
  dpd[n - 1] = 0;
  for (int i = 1; i < n; i++)
    dpi[i] = max(dpi[i - 1] + arr[i - 1] + 1 - arr[i], (long long int)0);
  for (int i = (int)n - 2; i >= 0; i--)
    dpd[i] = max(dpd[i + 1] + arr[i + 1] + 1 - arr[i], (long long int)0);
  long long int sumi[n], sumd[n];
  sumi[0] = dpi[0];
  sumd[n - 1] = dpd[n - 1];
  for (int i = 1; i < n; i++) sumi[i] = sumi[i - 1] + dpi[i];
  for (int i = (int)n - 2; i >= 0; i--) sumd[i] = sumd[i + 1] + dpd[i];
  long long int ans = numeric_limits<long long int>::max();
  for (int i = 1; i < n - 1; i++)
    ans = min(ans, sumi[i - 1] + sumd[i + 1] + max(dpi[i], dpd[i]));
  ans = min({sumd[0], sumi[n - 1], ans});
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
}
