#include <stdio.h>

int main(void) {
	int n[100001] = { 0 };
	int an[100001] = { 0 };
	int num = 0;

	for (int i = 0; i < 100001; i++) {
		n[i] = 2;
	}
	n[0] = 1;

	scanf("%d", &num);
	for (int i = 0; i <= num; i++) {
		int tmp = 0;
		scanf("%d", &tmp);
		an[i] = tmp;
	}

	int aa = 1;
	for (int i = 1; i <= num; i++) {
		aa *= 2;
	}

	int bb = 2;
	for (int i = 0; n[num] != aa; i++) {
		if (n[i] == an[i]) {
			break;
		}
		else {
			if (i == num) {
				for (int j = 0; j <= num; j++) {
					if (j == bb) {
						n[j] += 1;
					}
					else if (i > bb) {
						n[j] += 2;
					}
				}
				i = bb;
				bb++;
			}
		}
	}

	if ((n[num] == aa)||(n[num]+1 == aa)) {
		printf("-1");
	}
	else {
		int result = 0;
		for (int i = 0; i <= num; i++) {
			result += n[i];
		}
		printf("%d", result);
	}

	return 0;
}