#include <bits/stdc++.h>
using namespace std;
int n, mas[100005], ans[100005];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> mas[i];
    ans[mas[i]] = i + 1;
  }
  int maxx = 0, p = 1, ind = ans[1];
  for (int i = 2; i <= n; ++i) {
    if (ans[i] > ind)
      p++;
    else {
      maxx = max(p, maxx);
      p = 1;
    }
    ind = ans[i];
  }
  cout << n - max(p, maxx);
  return 0;
}
