#include <bits/stdc++.h>
using namespace std;
int a[100010], b[100010], ja[100010];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ja[a[i]] = i;
  }
  for (int i = 2; i <= n; i++) {
    if (ja[i] > ja[i - 1])
      b[i] = 1;
    else
      b[i] = 0;
  }
  int ad = 0;
  for (int i = 1; i <= n; i++) {
    int j = i;
    while (b[j] == 1) j++;
    ad = max(ad, j - i + 1);
    i = j;
  }
  cout << n - ad << endl;
}
