#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long n, x, ma = -1, cnt = 0;
  cin >> n;
  map<long long, long long> a;
  for (long long i = 0; i < n; i++) {
    cin >> x;
    while (a.find(x) != a.end()) {
      a.erase(x);
      cnt--;
      x++;
    }
    a[x] = 1;
    cnt++;
    ma = max(ma, x);
  }
  cout << ma - cnt + 1;
}
