program savey;
var q:array[1..1000000]of dword;
d:array[1..1000000]of longint;
t,k,m,n,i,j:longint;
an:dword;
begin
assign(input,'savey.in');
reset(input);
assign(output,'savey.out');
rewrite(output);
readln(n);
for t:=1 to n do
begin
read(i);
if i=1 then
an:=an+(1 shl(t-1));
end;
i:=0;
j:=1;
q[1]:=an;
repeat
inc(i);
an:=q[i]xor 1;
if an=0 then
begin
writeln(d[i]+1);
close(output);
halt;
end;
for t:=1 to j do
if q[t]=an then
break;
if q[t]<>an then
begin
inc(j);
q[j]:=an;
d[j]:=d[i]+1;
end;
for t:=1 to 30 do
if (q[i] shr (t-1))and 1=1 then
break;
an:=q[i] xor(1 shl (t));
if an=0 then
begin
writeln(d[i]+1);
close(output);
halt;
end;
for t:=1 to j do
if q[t]=an then
break;
if q[t]<>an then
begin
inc(j);
q[j]:=an;
d[j]:=d[i]+1;
end;
until i=j;
end.