#include <bits/stdc++.h>
int main() {
  int n, retval = 0;
  if (scanf("%d", &n) < 1) {
    exit(1);
  }
  int a[n];
  for (int i = 0; i < n; i++) {
    if (scanf("%d", &a[i]) < 1) {
      exit(1);
    }
  }
  for (int i = 1; i < n; i++) {
    while (a[i - 1] > a[i]) {
      a[i] += 1;
      retval++;
    }
  }
  printf("%d\n", retval);
  return 0;
}
