#include <bits/stdc++.h>
using namespace std;
const int N = 500050;
int n, A[N], pos[N];
bool used[N];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); i++) {
    scanf("%d", &A[i]);
    pos[A[i]] = i;
  }
  pos[n + 1] = -1;
  int best = 0;
  for (int k = 1; k <= n; k++) {
    if (used[k]) continue;
    int t = k, l = 1;
    for (int p = pos[t]; pos[t + 1] > p; p = pos[++t]) {
      l++;
      used[p] = true;
    }
    best = max(best, l);
  }
  printf("%d\n", n - best);
  return 0;
}
