#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int a[100005], c[2][100005], n, b[100005];
long long dp[2][100005], ans = 0x3f3f3f3f3f3f3f3f;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) b[i] = a[i];
  for (int i = 1; i <= n; i++) {
    if (b[i] > b[i - 1]) {
      dp[0][i] = dp[0][i - 1];
      c[0][i] = b[i];
    } else {
      long long need = b[i - 1] + 1 - b[i];
      dp[0][i] = dp[0][i - 1] + need;
      c[0][i] = b[i - 1] + 1;
      b[i] = b[i - 1] + 1;
    }
  }
  for (int i = 1; i <= n; i++) b[i] = a[i];
  for (int i = n; i >= 1; i--) {
    if (b[i] > b[i + 1]) {
      dp[1][i] = dp[1][i + 1];
      c[1][i] = b[i];
    } else {
      long long need = b[i + 1] + 1 - b[i];
      dp[1][i] = dp[1][i + 1] + need;
      c[1][i] = b[i + 1] + 1;
      b[i] = b[i + 1] + 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    long long ret = max(c[0][i - 1], c[1][i + 1]) + 1 - a[i];
    ret = max((long long)0, ret);
    ans = min(ans, ret + dp[0][i - 1] + dp[1][i + 1]);
  }
  printf("%lld\n", ans);
  return 0;
}
