#include <bits/stdc++.h>
using namespace std;
long n, i, k;
long a[100001], b[100001], mas[100001], ans[100001];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]] = i;
  }
  for (i = 1; i <= n; i++)
    if (mas[a[i] - 1] == 0)
      mas[a[i]]++;
    else
      mas[a[i]] = mas[a[i] - 1] + 1;
  for (i = 2; i <= n; i++)
    if (mas[i] > mas[1]) mas[1] = mas[i];
  k = 1;
  for (i = 2; i <= n; i++)
    if (b[i] > b[i - 1])
      ans[i] = i - k + 1;
    else
      k = i;
  for (i = 2; i <= n; i++)
    if (ans[i] > ans[1]) ans[1] = ans[i];
  cout << n - mas[1] << endl;
}
