#include <bits/stdc++.h>
using namespace std;
int pos[100005];
int lis[100005];
int a[100005];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) lis[i] = 1;
  int l = 0;
  l = 1;
  for (int i = 2; i <= n; i++) {
    if (pos[i] > pos[i - 1])
      lis[i] = (1 + lis[i - 1]);
    else
      lis[i] = 1;
    l = max(l, lis[i]);
  }
  cout << n - l;
  return 0;
}
