#include <iostream>
#include <algorithm>
using namespace std;

int N, A[200009], B[200009];

int main() {
	cin >> N;
	for (int i = 1; i <= N; i++) { cin >> A[i]; B[A[i]] = i; }
	int s = 0, maxn = 1;
	for (int i = 1; i <= N - 1; i++) {
		if (B[i] > B[i + 1]) s = i;
		maxn = max(maxn, i - s + 1);
	}
	cout << N - maxn << endl;
	return 0;
}