#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  map<long long, long long> ma;
  for (long long i = 0; i < n; i++) {
    long long z;
    cin >> z;
    ma[z]++;
    while (ma[z] == 2) {
      ma.erase(ma.find(z));
      ma[++z]++;
    }
  }
  long long maxi = (*ma.rbegin()).first;
  maxi++;
  cout << maxi - (long long)ma.size() << "\n";
  return 0;
}
