#include <bits/stdc++.h>
using namespace std;
long long I[100005], D[100005];
int Array[100005], Front[100005], Back[100005];
int main() {
  int N, i, previous;
  long long Ans = 1e18;
  scanf("%d%d", &N, Array + 1);
  I[1] = 0LL;
  previous = 1LL * Array[1];
  Front[1] = Array[1];
  for (i = 2; i <= N; ++i) {
    scanf("%d", Array + i);
    if (Array[i] > previous) {
      I[i] = I[i - 1];
      previous = Array[i];
    } else {
      I[i] = I[i - 1] + previous - Array[i] + 1;
      ++previous;
    }
    Front[i] = previous;
  }
  previous = Array[N];
  D[N] = 0LL;
  Back[N] = Array[N];
  for (i = N - 1; i >= 1; --i) {
    if (Array[i] > previous) {
      D[i] = D[i + 1];
      previous = 1LL * Array[i];
    } else {
      D[i] = D[i + 1] + previous - Array[i] + 1;
      ++previous;
    }
    Back[i] = previous;
  }
  for (i = 1; i <= N - 1; ++i) {
    if (Front[i] != Back[i + 1])
      Ans = min(Ans, I[i] + D[i + 1]);
    else
      Ans = min(Ans, I[i] + D[i + 1] + 1);
  }
  Ans = min(Ans, min(D[1], I[N]));
  printf("%lld\n", Ans);
  return 0;
}
