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