#include <bits/stdc++.h>
using namespace std;
int pos[100005];
int main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int n;
  cin >> n;
  int i;
  int j;
  for (i = 0; i < n; i++) {
    cin >> j;
    pos[j] = i;
  }
  int prev = 0;
  int l = 0;
  int ans = 0;
  for (i = n; i > 0; i--) {
    if (pos[i] < prev) {
      l++;
      prev = pos[i];
    } else {
      ans = max(ans, l);
      l = 1;
      prev = pos[i];
    }
  }
  ans = max(ans, l);
  cout << n - ans << endl;
}
