#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n;
  long long h, w, x;
  long long k;
  long long d;
  string s;
  char box[20000];
  long long a[20000] = {0};
  long long count = 0;
  long long all = 0;
  bool flag = true;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n; i++) {
    if (i == 0) {
    } else {
      for (long long j = 0; a[i - 1] > a[i]; j++) {
        a[i]++;
        all++;
      }
    }
  }
  cout << all << "\n";
}
