#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e6 + 5;
const long long inf = 1e10;
const long long mod = 1e9 + 7;
long long n;
long long a[maxN];
long long f[1000005];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    f[a[i]] = i;
  }
  long long res = 1;
  long long tam = 1;
  for (long long i = 2; i <= n; i++) {
    if (f[i] > f[i - 1])
      tam++;
    else {
      res = max(res, tam);
      tam = 1;
    }
  }
  res = max(res, tam);
  cout << n - res;
  return 0;
}
