program mountain;
Uses sysutils, Math;

const
    MAXN = 100005;

var
    ANS, N, i, j,h, maxMountainLength, lung : LongInt;
    P, leftLIS, rightLIS  : Array[0..MAXN-1] of LongInt;
    rimossi : string;
    rimosso: boolean;
begin

  

    ReadLn(N);
    rimossi:=''; lung:=N;
    for i:=0 to N-1 do begin
                        Read(P[i]);
                        rimossi:=rimossi+IntTostr(P[i]);
                       end;  
    ReadLn();
    for i:=1 to lung do  write(rimossi[i],'  '); writeln;
    rimosso:=false; i:=2; h:=1;
    while h<5 do 
       begin
        i:=2; rimosso:=false;
        while i<lung do
              begin
                if  (rimossi[i]<rimossi[i-1]) and (rimossi[i]<rimossi[i+1]) then
                                                                              begin
                                                                                delete(rimossi,i,1);
                                                                                lung:=lung-1;
                                                                                rimosso:=true;
                                                                              end;
              i:=i+1;
              end;
           h:=h+1;   
         end;     
    for i:=1 to lung  do begin  P[i-1]:=StrToInt(rimossi[i]); write(P[i-1],' ');end;
     end. 
    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*)

   for i:=0 to  N-1 do begin leftLIS[i]:=1; rightLIS[i]:=1; rimosso[i]:=0; end;

	(*Calculate LIS from left to right for each position*)
   for i:=1 to N-2 do if  (P[i]<P[i-1]) and (P[i]<P[i+1]) then rimosso[i]:=1; 
   for i := 1 to N-1 do
         if (rimosso[i]=0) then 
             for j:= 0 to i-1 do
                begin
                  if (rimosso[j]=0) and (P[i] > P[j]) then leftLIS[i] := max(leftLIS[i], leftLIS[j] + 1);                          
                end;  
  
	(* Calculate LIS from right to left (decreasing subsequence) for each position*)

   for i := N - 2 downto 0 do
        if (rimosso[i]=0) then 
            for  j := i + 1 to N-1 do
               begin
                if (rimosso[j]=0) and (P[i] > P[j]) then rightLIS[i] := max(rightLIS[i], rightLIS[j] + 1);                
	       end;
   
        (* Find the maximum length of mountain subsequence*)
   maxMountainLength := 0;
   for i := 0 to N-1 do
	(*A valid mountain peak must have at least one element on both sides*)
	(*leftLIS[i] > 1 ensures there's at least one element before peak*)
	(*rightLIS[i] > 1 ensures there's at least one element after peak*)
   if (leftLIS[i] >= 1) and (rightLIS[i] >= 1)  then
  	(*Total mountain length with peak at i Subtract 1 because peak is counted in both leftLIS and rightLIS*)
         maxMountainLength := max(maxMountainLength, leftLIS[i] + rightLIS[i] - 1);
 	(* Minimum removals = total elements - maximum mountain length*)
   ANS:= N - maxMountainLength; 
   WriteLn(ANS);
end.