#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> mas(n);
  vector<int> p(n + 1);
  for (int i = 0; i < n; ++i) {
    cin >> mas[i];
    p[mas[i]] = i;
  }
  int len = 0;
  int tmp = 0;
  for (int i = 2; i <= n; ++i) {
    if (p[i] > p[i - 1]) {
      tmp++;
    } else {
      if (tmp + 1 > len) {
        len = tmp + 1;
      }
      tmp = 0;
    }
  }
  if (tmp + 1 > len) {
    len = tmp + 1;
  }
  cout << (n - len) << endl;
}
