#include <bits/stdc++.h>
using namespace std;
map<long long, int> tree;
map<long long, int>::iterator iter;
int a;
long long s;
int z[120000];
void ins(long long aa) {
  iter = tree.find(aa);
  if (iter == tree.end()) {
    tree.insert(make_pair(aa, 0));
  } else {
    tree.erase(iter);
    ins(aa + 1);
  }
}
int main() {
  scanf("%d", &a);
  memset(z, 0, sizeof(z));
  for (int q = 0; q < a; q++) {
    scanf("%I64d", &s);
    ins(s);
  }
  iter = tree.end();
  iter--;
  printf("%I64d\n", (iter->first) + 1 - tree.size());
  return 0;
}
