program zzn;
type
rec=record
st,a:longint;
end;
arr=array[1..30]of byte;
var
head,tail,n,i,j:longint;
list:array[1..100000]of rec;
v:array[0..100000000]of boolean;
aa:arr;
ch:char;
procedure init;
begin
assign(input,'savey.in');
reset(input);
assign(output,'savey.out');
rewrite(output);
readln(n);
for i:=1 to n do
begin
read(aa[i]);
end;
list[1].st:=0;
tail:=1;head:=0;
end;
procedure closef;
begin
close(input);
close(output);
end;
function er(c:arr):longint;
var
i,j:longint;
begin
j:=0;
for i:=n downto 1 do
begin
j:=j+c[n-i+1]*trunc(exp((i-1)*ln(2)));
end;
exit(j);
end;
procedure change(x:longint;var c:arr);
var
i:integer;
begin
fillchar(c,sizeof(c),0);
i:=n+1;
repeat
dec(i);
c[i]:=x mod 2;
x:=x shr 1;
until x=0;
end;
function can(x:integer;c:arr):boolean;
var
i:integer;
begin
if x=2 then
begin
if c[1]=1 then exit(true) else exit(false);
end;
for i:=1 to x-2 do if c[i]=1 then exit(false);
if c[x-1]=0 then exit(false);
exit(true);
end;
procedure main;
var
x,y:longint;
c,b:arr;
begin
list[1].a:=er(aa);
fillchar(v,sizeof(v),0);
v[list[1].a]:=true;
repeat
inc(head);
x:=list[head].a;
change(x,c);
for i:=2 to n do
begin
b:=c;
if can(i,b) then
begin
b[i]:=1-b[i];
y:=er(b);
if not(v[y])then
begin
v[y]:=true;
inc(tail);
list[tail].a:=y;
list[tail].st:=list[head].st+1;
if y=0 then
begin
writeln(list[tail].st);
exit;
end;
end;
end else if i<>2 then break;
end;
b:=c;
b[1]:=1-c[1];
y:=er(b);
if not(v[y]) then
begin
v[y]:=true;
inc(tail);
list[tail].a:=y;
list[tail].st:=list[head].st+1;
if y=0 then
begin
writeln(list[tail].st);
exit;
end;
end;
until head>=tail;
end;
begin
init;
main;
closef;
end.