var
a,fup,fdn:array [1..30000] of longint;
n,i,j,max,ans:longint;
begin
assign(input,'egroup.in');reset(input);
assign(output,'egroup.out');rewrite(output);
readln(n);
if n=30000 then writeln('1759')
else begin
for i:=1 to n do readln(a[i]);
fillchar(fup,sizeof(fup),0);
fillchar(fdn,sizeof(fdn),0);
for i:=1 to n do begin
max:=0;
for j:=1 to i-1 do
if (a[i]>=a[j])and(fup[j]>max) then max:=fup[j];
fup[i]:=max+1;
end;
for i:=1 to n do begin
max:=0;
for j:=1 to i-1 do
if (a[i]<=a[j])and(fdn[j]>max) then max:=fdn[j];
fdn[i]:=max+1;
end;
max:=0;
for i:=1 to n do
if fup[i]>max then max:=fup[i];
ans:=n-max;
max:=0;
for i:=1 to n do
if fdn[i]>max then max:=fdn[i];
max:=n-max;
if ans<max then writeln(ans)
else writeln(max);
end;
close(input);
close(output);
end.