#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, c = 1, max = 1;
  cin >> n;
  int a[n];
  int pos[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    pos[a[i] - 1] = i;
  }
  for (i = 0; i < n - 1; i++) {
    if (pos[i] < pos[i + 1]) {
      c++;
    } else {
      max = c > max ? c : max;
      c = 1;
    }
  }
  max = c > max ? c : max;
  cout << n - max;
  return 0;
}
