Const
max=205;
inf=99999999;
Var
i,j,k,n,k1:longint;
h,opt:array[1..max]of longint;
t:array[0..32]of longint;
flag:boolean;
Begin
assign(input,'ladder.in');
assign(output,'ladder.out');
reset(input);
rewrite(output);
flag:=true;
readln(n);
t[0]:=1;
for i:=1 to 30 do
t[i]:=t[i-1] shl 1;
for i:=1 to n do
read(h[i]);
for i:=2 to n do
if flag then
begin
opt[i]:=inf;
if h[i]=h[i-1]+1 then
opt[i]:=opt[i-1]+1;
for j:=1 to i-1 do
begin
if j-30>1 then
k1:=j-30
else
k1:=1;
for k:=k1 to j-1 do
if ((h[k]+t[j-k]>=h[i])and(opt[i]>opt[j]+j-k+1))then
opt[i]:=opt[j]+j-k+1;
end;
if opt[i]=inf then
flag:=false;
end;
if flag then
writeln(opt[n])
else
writeln(-1);
close(input);
close(output);
End.