#include <bits/stdc++.h>
using namespace std;
const int N = 1000060;
const int mod = (int)1e9 + 7;
const int MX = 200020;
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n;
  cin >> n;
  int a[n], idx[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
    idx[a[i]] = i;
  }
  int mx_consec = 0;
  for (int i = 0; i < n; ++i) {
    int j = i;
    while (j + 1 < n && idx[j] < idx[j + 1]) j++;
    mx_consec = max(mx_consec, j - i + 1);
    i = j;
  }
  cout << n - mx_consec << endl;
  return 0;
}
