program mountain;
Uses sysutils, Math;

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

var
    ANS, N, i, id, x, maxMountainLength, len : LongInt;
    P, leftLIS, rightLIS, index: Array[0..MAXN-1] of LongInt;
    LIS : elenco;
   
   
   

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
   

    ReadLn(N);
  
    for i:=0 to N-1 do begin Read(P[i]); index[i]:=-1; end;
                        
    ReadLn();
    ANS := 0; 
    maxMountainLength := 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]; index[0]:=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];
                                     index[len-1]:=i;
                                     leftLIS[i]:=len;
                                   end
                                 else
                                    begin
                                      // if element is to be inserted in lis
                                      if (id<>0) and ((P[index[id]]<P[index[id]-1]) and (P[index[id]]<P[index[id]+1])) then
                                                     begin
                                                        LIS[id] := P[i];
                                                        leftLIS[i]:=id+1;                                                       
                                                     end
                                               else 
                                                    leftLIS[i]:=0;
                                               
                                    end;                   
                    end; 
   
       (* Calculate LIS from right to left (decreasing subsequence) for each position*)
  
   len:=1; SetLength(LIS,len); LIS[0]:=P[N-1]; index[0]:=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];
                                     index[len-1]:=i;
                                     rightLIS[i]:=len;
                                   end
                                 else
                                    begin
                                      // if element is to be inserted in lis
                                      if (id<>0) and ((P[index[id]]<P[index[id]-1]) and (P[index[id]]<P[index[id]+1])) then
                                                     begin
                                                        LIS[id] := P[i];
                                                        rightLIS[i]:=id+1;                                                        
                                                     end
                                                else  
                                                   rightLIS[i]:=0;
                                                
                                                 
                                   end;                    
                    end;
     (* 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.