#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, g, a[100001], p[100001], maxi;
  vector<long long> v;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    p[a[i]] = i + 1;
  }
  g = 1;
  for (i = 1; i < n; i++) {
    if (p[i] <= p[i + 1]) {
      g++;
    } else {
      v.push_back(g);
      g = 1;
    }
  }
  v.push_back(g);
  maxi = 0;
  for (i = 0; i < v.size(); i++) {
    maxi = max(maxi, v[i]);
  }
  cout << n - maxi;
}
