#include <bits/stdc++.h>
using namespace std;
int _;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i += 1) cin >> a[i];
  for (int i = 0; i < n; i += 1) --a[i];
  vector<int> v(n);
  for (int i = 0; i < n; i += 1) {
    v[a[i]] = i;
  }
  int counter = 1;
  int b[n];
  for (int i = 0; i < n; i += 1) b[i] = -1;
  b[0] = 1;
  for (int i = 1; i < n; i += 1) {
    if (b[i] != -1)
      continue;
    else if (v[i] > v[i - 1])
      b[i] = 1 + b[i - 1];
    else
      b[i] = 1;
    counter = max(counter, b[i]);
  }
  cout << n - counter << endl;
  return 0;
}
