#include <bits/stdc++.h>
using namespace std;
int a[100010], b[100010];
int main() {
  int n;
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      b[a[i]] = i;
    }
    int len = 1, ans = 0;
    for (int i = 2; i <= n; i++) {
      if (b[i] > b[i - 1])
        len++;
      else {
        ans = ans > len ? ans : len;
        len = 1;
      }
    }
    ans = ans > len ? ans : len;
    printf("%d\n", n - ans);
  }
  return 0;
}
