#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const int MAXN = 2e5 + 5;
const int MAXM = 1e4 + 500;
const int N = 500 + 5;
const double EPS = 1e-9;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
long long i, j, d;
long long n, k, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  set<int> A;
  for (i = 0; i < n; i++) {
    int first;
    cin >> first;
    while (A.find(first) != A.end()) {
      A.erase(first);
      first++;
    }
    A.insert(first);
  }
  int R = *A.rbegin();
  cout << (R + 1) - A.size();
  return 0;
}
