#include <bits/stdc++.h>
using namespace std;
struct node {
  int x;
  node(int xx) { x = xx; }
};
bool operator<(node const &a, node const &b) {
  if (a.x > b.x)
    return true;
  else
    return false;
}
priority_queue<node> p;
int main() {
  int n;
  scanf("%d", &n);
  int tmp;
  for (int i = 0; i < n; i++) {
    scanf("%d", &tmp);
    p.push(node(tmp));
  }
  int cnt = -1;
  int ans = 0;
  node ntmp(0);
  node nttmp(0);
  while (!p.empty()) {
    ntmp = p.top();
    p.pop();
    if (!p.empty())
      nttmp = p.top();
    else
      nttmp.x = -1;
    if (nttmp.x == ntmp.x) {
      p.pop();
      p.push(node(ntmp.x + 1));
      continue;
    }
    ans += ntmp.x - cnt - 1;
    cnt = ntmp.x;
  }
  printf("%d\n", ans);
  return 0;
}
