#include <bits/stdc++.h>
using namespace std;
string itosm(long long x) {
  if (x == 0) return "0";
  string ans = "";
  while (x > 0) {
    ans += ((x % 10) + '0');
    x /= 10;
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
long long stoim(string str) {
  long long ans = 0;
  long long k = 1;
  for (int i = str.length() - 1; i >= 0; i--) {
    ans += (str[i] - '0') * k;
    k *= 10;
  }
  return ans;
}
const long long infll = 1e18 + 3;
const int inf = 1009000999;
const double eps = 1e-7;
const int maxn = 1e6 + 777;
const int baseint = 1000200013;
const long long basell = 1e18 + 3;
const long double PI = acos(-1.0);
const long long mod = 1e9 + 7;
long long a[maxn];
long long b[maxn];
void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long ans = infll, sum = 0;
  b[n - 1] = a[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] <= b[i + 1]) {
      b[i] = b[i + 1] + 1;
      sum += (b[i] - a[i]);
    } else
      b[i] = a[i];
  }
  sum -= (b[0] - a[0]);
  b[0] = a[0];
  if (b[0] == b[1]) {
    ans = sum + 1;
  } else
    ans = sum;
  for (int i = 1; i < n; i++) {
    sum -= (b[i] - a[i]);
    if (a[i] <= b[i - 1])
      b[i] = b[i - 1] + 1;
    else
      b[i] = a[i];
    if (b[i] == b[i + 1]) sum++;
    sum += (b[i] - a[i]);
    ans = min(ans, sum);
    if (b[i] == b[i + 1]) sum--;
  }
  cout << ans;
}
int main() {
  srand(time(0));
  ios_base::sync_with_stdio(0);
  ;
  solve();
  return 0;
}
