#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, bool> PIB;

int N;
PIB A[100000];
int cache[100000][32];

int f(int i, int j) {
  if(i >= N) return 0;
  int &ret = cache[i][j];
  if(ret < 0) {
    if(A[i].second) {
      // complete
      ret = max(A[i].first - j, 0) + f(i + 1, A[i].first);
    } else {
      // incomplete
      ret = min(
          max(A[i].first - j, 0) + f(i + 1, A[i].first - 1),
          max(A[i].first - 1 - j, 0) + 1 + f(i + 1, A[i].first));
    }
  }
  return ret;
}

int main() {
  memset(cache, 0xFF, sizeof(cache));
  scanf("%d", &N);
  for(int i = 0; i < N; i++) {
    int m;
    scanf("%d", &m);
    for(int j = 0; ; j++) {
      if(m <= (1 << j)) {
        if(m == (1 << j)) {
          A[i] = PIB(j, true);
        } else {
          A[i] = PIB(j, false);
        }
        break;
      }
    }
  }
  printf("%d\n", f(0, 0));
  return 0;
}