#include <bits/stdc++.h>
using namespace std;
set<int> num;
int n;
int main() {
  while (scanf("%d", &n) != EOF) {
    int a;
    num.clear();
    int maxn = -0x3f3f3f3f;
    while (n--) {
      scanf("%d", &a);
      while (num.count(a)) {
        num.erase(a);
        a++;
      }
      num.insert(a);
      maxn = max(maxn, a);
    }
    printf("%d\n", maxn + 1 - num.size());
  }
}
