#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int A[n + 1];
  for (int i = 1; i <= n; ++i) {
    int a;
    scanf("%d", &a);
    A[a] = i;
  }
  int ma = 1;
  int rec = 1;
  for (int i = 2; i <= n; ++i) {
    if (A[i] > A[i - 1]) {
      ++rec;
      ma = max(ma, rec);
    } else {
      rec = 1;
    }
  }
  cout << (n - ma) << endl;
  return 0;
}
