#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+10;
int n,a[N],f[N];
void solve(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int cnt=0;
for(int i=1;i<=n;i++){
if(a[i]>f[cnt]){//如果这个数比以前的大说明不破坏数列严格递增的性质
//那么将这个数放在数列都后面
f[++cnt]=a[i];
}
else{
*lower_bound(f+1,f+1+cnt,a[i])=a[i];//如果这个数比之前的数小
//那么用lower_bound()函数找到它适合的位置将这个数放进去
}
}
cout<<n-cnt<<"\n";
// for(int i=1;i<=cnt;i++){
// cout<<f[i]<<" ";
// }
}
signed main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
}