#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[200005],b[200005];
int ans;
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        a[x]=i;
    }
    for(int i=1;i<=n-1;i++)
    {
        if(a[i]<a[i+1])
        {
            b[i+1]+=b[i]+1;
            ans=max(ans,b[i+1]);
        }
    }
    ans=n-ans-1;
    cout<<ans<<endl;
    return 0;
}
