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