#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int arr[maxn];
int flag[maxn];
stack<int> s1;
void solve() {
  int n;
  scanf("%d", &n);
  int val = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    if (arr[i] == (i - 1)) {
      val = i;
    }
  }
  for (int i = 1; i <= val; i++) {
    flag[i] = 1;
  }
  int cnt = val;
  for (int i = val + 1; i <= n; i++) {
    if (arr[i] >= cnt) {
      flag[i] = 1;
      if (arr[i] > cnt) {
        for (int j = 1; j <= (arr[i] - cnt); j++) {
          flag[s1.top()] = 1;
          s1.pop();
        }
      }
      cnt += (arr[i] - cnt);
      cnt++;
    } else {
      s1.push(i);
    }
  }
  cnt = 0;
  long long int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (cnt - arr[i]);
    if (flag[i])
      cnt++;
    else
      ans--;
  }
  printf("%lld\n", ans);
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
