#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, x;
  scanf("%d", &n);
  int p[101000];
  for (int i = 0; i < n; i++) {
    scanf("%d", &x), p[x - 1] = i;
  }
  int cur = 1;
  int ans = 1;
  for (int i = 1; i < n; i++) {
    if (p[i] > p[i - 1])
      cur++;
    else
      cur = 1;
    ans = max(ans, cur);
  }
  printf("%d\n", n - ans);
  return 0;
}
