#include <bits/stdc++.h>
using namespace std;
int go[100100];
int p[100100];
int n;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    p[x] = i;
  }
  go[n] = 1;
  for (int i = n - 1; i >= 1; i--)
    if (p[i] < p[i + 1])
      go[i] = go[i + 1] + 1;
    else
      go[i] = 1;
  int ret = 1000000000;
  for (int i = 0; i < n; i++) {
    ret = min(ret, n - go[i + 1]);
  }
  cout << ret;
  return 0;
}
