#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define LL long long
using namespace std;
const int maxn=(int)1e5+10;
int n,a[maxn],f[maxn],loc[maxn];
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        loc[a[i]]=i;
    }
    int cnt=1,r=0;
    for(int i=1;i<=n;i++){
        r=loc[i];int j;
        for(j=i+1;loc[j]>r;j++){r=loc[j];}
        cnt=max(cnt,j-i);i=j-1;
    }
    cout<<n-cnt;
    return 0;
}