#include <bits/stdc++.h>
using namespace std;
int h[1000000] = {0};
int main() {
  int a;
  cin >> a;
  for (int i = 1; i <= a; i++) {
    int p;
    scanf("%d", &p);
    h[p] = i;
  }
  int len = 1, ans = 1;
  for (int i = 2; i <= a; i++) {
    if (h[i] > h[i - 1])
      ans = max(ans, ++len);
    else
      len = 1;
  }
  cout << a - ans << endl;
  return 0;
}
