#include <bits/stdc++.h>
using namespace std;
int main() {
  int sum, N;
  cin >> N;
  sum = 0;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    while (A[i] > A[i + 1]) {
      A[i + 1]++;
      sum++;
    }
  }
  cout << sum << endl;
}
