program mountain;
Uses sysutils, Math;

const
    MAXN = 100005;
Type elenco= Array of LongInt;
var
    ANS, N, i, j, id,x, maxMountainLength, lung, len : 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();
    i:=2; uscita:=false;
    while uscita=false do 
       begin
        i:=2; uscita:=true;
        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;  
                                                uscita:=false;                                          
                                            end;
                i:=i+1;
              end;              
       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 then
                                      begin
                                        LIS[id] := P[i];
                                        leftLIS[i]:=id+1;
                                      end 
                       // if element in not present in lis insert at the end
                                    else
                                      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 then
                                      begin
                                        LIS[id] := P[i];
                                        leftLIS[i]:=id+1;
                                      end 
                       // if element in not present in lis insert at the end
                                    else
                                      begin
                                       len:=len+1;
                                       SetLength(LIS,len);
                                       LIS[len] := P[i];
                                       leftLIS[i]:=len;
                                      end; 
                    end;
                    
    maxMountainLength := 0;
    (* Find the maximum length of mountain subsequence*)
    // for every index check for longest mountain array,
    for i := 1 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
   
   ANS:= N - maxMountainLength; 
   WriteLn(ANS);
end.
