#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  long long res = 0;
  cin >> n;
  vector<bool> v(6000, false);
  for (int i = 0; i < n; ++i) {
    cin >> k;
    if (!v[k - 1])
      v[k - 1] = true;
    else
      while (v[k - 1]) {
        ++res;
        ++k;
        v[k - 2] = true;
      }
    v[k - 1] = true;
  }
  cout << res;
}
