#include <bits/stdc++.h>
using namespace std;
const int MAXN = 123456;
void pre();
void post();
int n;
int mas[MAXN];
int solve[MAXN];
void read() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &mas[i]);
    solve[mas[i] - 1] = i;
  }
}
int main() {
  pre();
  read();
  int ans = 0;
  int l = 0;
  for (int i = 1; i < n; i++) {
    if (solve[i] < solve[i - 1]) {
      l = i;
    }
    ans = max(ans, i - l + 1);
  }
  ans = max(ans, n - l);
  cout << n - ans;
  post();
  return 0;
}
void pre() {}
void post() {}
