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