#include <bits/stdc++.h>
using namespace std;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int main() {
  int n;
  scanf("%d", &n);
  int a[n + 1];
  vector<long long int> v;
  map<int, int> ase;
  int mx = -1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    int now = a[i];
    ase[now]++;
    mx = max(now, mx);
    while (ase[now] == 2) {
      ase.erase(now);
      ase[++now]++;
      mx = max(now, mx);
    }
  }
  cout << (mx + 1 - ase.size());
  return 0;
}
