#include <bits/stdc++.h>
using namespace std;
int a[105000];
int pos[105000];
int main() {
  int n;
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      pos[a[i]] = i;
    }
    if (n == 1) {
      printf("0\n");
      continue;
    }
    int output = n;
    int now = 1;
    while (now < n) {
      int u = now;
      int v;
      int tmp = 1;
      while (1) {
        v = u + 1;
        if (pos[v] > pos[u]) {
          tmp++;
          u++;
        } else
          break;
      }
      output = min(output, n - tmp);
      now = u + 1;
    }
    printf("%d\n", output);
  }
}
