#include<bits/stdc++.h>
using namespace std;

int n,a[200005],p[200005];
int main() {
	cin>>n;
	for (int i=1;i<=n;i++) {
		cin>>a[i];
		p[a[i]]=i;
	}
	int ans=1,s=1;
	for (int i=2;i<=n;i++) if (p[i-1]<p[i]) {
		s++; ans=max(ans,s);
	} else s=1;
	ans=n-ans;
	cout<<ans<<endl;
	return 0;
}
