#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  scanf("%d", &k);
  int a_now = 0;
  int digit_now = 0;
  int required = 0;
  for (int i = 0; i < k; ++i) {
    int a;
    scanf("%d", &a);
    if (a > a_now) {
      for (int d = a_now; d < a; ++d) {
        if (digit_now % 2 == 0) {
          ++required;
        }
        digit_now /= 2;
        if (digit_now == 0) {
          required += a - d - 1;
          break;
        }
      }
      a_now = a;
    }
    ++digit_now;
  }
  for (; digit_now > 0; digit_now /= 2) {
    if (digit_now % 2 == 0) {
      ++required;
    }
  }
  printf("%d\n", required);
  return 0;
}
