program mountain;
Uses sysutils, Math;
const
MAXN = 100005;
Type elenco= Array of LongInt;
var
ANS, N, i, j,id,x, maxMountainLength, lung, len, ricordamaxmount : LongInt;
P, leftLIS, rightLIS : Array[0..MAXN-1] of LongInt;
LIS : elenco;
rimossi : Ansistring;
uscita : boolean;
Procedure ricercaUpper (var w:elenco; target:Longint); (*ritorna indice del valore maggiore/uguale a target oppure -1 se non esiste*)
var m,start,eend: Longint;
begin
start:=0; eend:=len-1 ; m:=-1;
while start<=eend do
begin
m:=(start + eend) div 2;
if w[m]<target then start:=m+1
else if w[m]>=target then begin id:=m; eend:=m-1 end;
end;
if start=len then id:=-1;
end;
begin
(*assign(input, 'input.txt'); reset(input);
assign(output, 'output.txt'); rewrite(output);*)
ReadLn(N);
rimossi:=''; lung:=N;
for i:=0 to N-1 do begin
Read(P[i]);
rimossi:=rimossi+IntTostr(P[i]);
end;
ReadLn();
ANS := 0; ricordamaxmount:=0;
(*leftLIS[i] stores the length of longest increasing subsequence ending at index i*)
(*rightLIS[i] stores the length of longest decreasing subsequence starting at index i*)
len:=1; SetLength(LIS,len); LIS[0]:=P[0];
for i:=0 to lung-1 do begin leftLIS[i]:=1; rightLIS[i]:=1; end;
(*Calculate LIS from left to right for each position*)
for i :=1 to lung-1 do
begin
ricercaUpper(Lis, P[i]);
// if element is to be inserted in lis
if (id <>-1) and (id<>0) then
begin
LIS[id] := P[i];
leftLIS[i]:=id+1;
end
// if element in not present in lis insert at the end
else
if id=-1 then
begin
len:=len+1;
SetLength(LIS,len);
LIS[len-1] := P[i];
leftLIS[i]:=len;
end;
end;
(* Calculate LIS from right to left (decreasing subsequence) for each position*)
len:=1; SetLength(LIS,len); LIS[0]:=P[N-1];
for i :=lung-2 downto 0 do
begin
ricercaUpper(Lis, P[i]);
// if element is to be inserted in lis
if (id <>-1) and (id<>0) then
begin
LIS[id] := P[i];
rightLIS[i]:=id+1;
end
// if element in not present in lis insert at the end
else
if id=-1 then
begin
len:=len+1;
SetLength(LIS,len);
LIS[len-1] := P[i];
rightLIS[i]:=len;
end;
end;
maxMountainLength := 0;
(* Find the maximum length of mountain subsequence*)
// for every index check for longest mountain array,
for i := 0 to lung-1 do
begin
if (leftLIS[i] >=1) AND (rightLIS[i] >= 1) then
begin
x := leftLIS[i] + rightLIS[i] - 1;
maxMountainLength := max(maxMountainLength, x);
end;
end;
// returning removals
if maxMountainLength>ricordamaxmount then ricordamaxmount:=maxMountainLength;
uscita:=false;
while uscita=false do
begin
j:=2; uscita:=true;
while j<lung do
begin
if (rimossi[j]<rimossi[j-1]) and (rimossi[j]<rimossi[j+1])
then
begin
delete(rimossi,j,1);
lung:=lung-1;
setLength(rimossi,lung);
uscita:=false;
end;
for i:=1 to lung do P[i-1]:=StrToInt(rimossi[i]);
ANS := 0;
(*leftLIS[i] stores the length of longest increasing subsequence ending at index i*)
(*rightLIS[i] stores the length of longest decreasing subsequence starting at index i*)
len:=1; SetLength(LIS,len); LIS[0]:=P[0];
for i:=0 to lung-1 do begin leftLIS[i]:=1; rightLIS[i]:=1; end;
(*Calculate LIS from left to right for each position*)
for i :=1 to lung-1 do
begin
ricercaUpper(Lis, P[i]);
// if element is to be inserted in lis
if (id <>-1) and (id<>0) then
begin
LIS[id] := P[i];
leftLIS[i]:=id+1;
end
// if element in not present in lis insert at the end
else
if id=-1 then
begin
len:=len+1;
SetLength(LIS,len);
LIS[len-1] := P[i];
leftLIS[i]:=len;
end;
end;
(* Calculate LIS from right to left (decreasing subsequence) for each position*)
len:=1; SetLength(LIS,len); LIS[0]:=P[N-1];
for i :=lung-2 downto 0 do
begin
ricercaUpper(Lis, P[i]);
// if element is to be inserted in lis
if (id <>-1) and (id<>0) then
begin
LIS[id] := P[i];
rightLIS[i]:=id+1;
end
// if element in not present in lis insert at the end
else
if id=-1 then
begin
len:=len+1;
SetLength(LIS,len);
LIS[len-1] := P[i];
rightLIS[i]:=len;
end;
end;
maxMountainLength := 0;
(* Find the maximum length of mountain subsequence*)
// for every index check for longest mountain array,
for i := 0 to lung-1 do
begin
if (leftLIS[i] >=1) AND (rightLIS[i] >= 1) then
begin
x := leftLIS[i] + rightLIS[i] - 1;
maxMountainLength := max(maxMountainLength, x);
end;
end;
// returning removals
if maxMountainLength>ricordamaxmount then ricordamaxmount:=maxMountainLength;
j:=j+1;
end;
end;
ANS:= N - ricordamaxmount;
WriteLn(ANS);
end.