#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + (ch & 15), ch = getchar();
  return f ? -x : x;
}
signed main() {
  int n = read();
  set<long long> s;
  long long mx = 0;
  for (long long i = 0, x; i < n; ++i) {
    x = read();
    while (s.count(x)) s.erase(x), ++x;
    s.insert(x), mx = max(mx, x);
  }
  printf("%lld\n", mx + 1 - s.size());
  return 0;
}
