program mountain;
Uses sysutils, Math;

const
    MAXN = 100005;
    
Type elenco= Array of LongInt;

var
    ANS, N, i,h, id, x, maxMountainLength, lung, len, count : LongInt;
    P, leftLIS, rightLIS, valli: 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]);
                        valli[i]:=0; 
                       end;  
    ReadLn();
    uscita:=false; h:=0; count:=0;
   
        i:=2; uscita:=true; 
        while i<lung do
              begin
                if  (rimossi[i]<rimossi[i-1]) and (rimossi[i]<rimossi[i+1])                    
                                      then
                                           begin
                                                valli[i+count+h-1]:=-1;
                                                delete(rimossi,i,1);
                                                lung:=lung-1;  count:=count+1;
                                                writeln (rimossi,' ',h,' ', count);
                                                setLength(rimossi,lung);
                                                uscita:=false; 
                                            end
                                      else i:=i+1;      
               
              end; 
          
          
    for i:=0 to N-1 do write(valli[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  N-1 do begin leftLIS[i]:=1; rightLIS[i]:=1; end;
    (*Calculate LIS from left to right for each position*)
    for i :=1 to N-1 do
                    begin
                        ricercaUpper(Lis, P[i]);
                       // if element in not present in lis insert at the end
                        
                        if id=-1 then
                                   begin
                                     len:=len+1;
                                     SetLength(LIS,len);
                                     LIS[len-1] := P[i];
                                     leftLIS[i]:=len;
                                   end
                                 else
                                    begin
                                      // if element is to be inserted in lis
                                      if (id<>0) and (valli[id]=-1) then
                                                     begin
                                                        LIS[id] := P[i];
                                                        leftLIS[i]:=id+1;                                                       
                                                     end
                                        
                                                   
                                    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 :=N-2  downto 0 do
                    begin
                        ricercaUpper(Lis, P[i]);
                        if id=-1 then
                                   begin
                                     len:=len+1;
                                     SetLength(LIS,len);
                                     LIS[len-1] := P[i];
                                     rightLIS[i]:=len;
                                   end
                                 else
                                    begin
                                      // if element is to be inserted in lis
                                      if (id<>0) and (valli[N-1-id]=-1) then
                                                     begin
                                                        LIS[id] := P[i];
                                                        rightLIS[i]:=id+1;
                                                        
                                                     end
                                        
                                                 
                                   end;                    
                    end;
                    
    maxMountainLength := 0;
    (* Find the maximum length of mountain subsequence*)
    // for every index check for longest mountain array,
    for i := 0 to N-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
   
   ANS:= N - maxMountainLength; 
   WriteLn(ANS);
end.