#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 7;
const int N = 2e5 + 10;
int a[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a[x] = i;
  }
  int maxn = 1;
  int cnt = 1;
  for (int i = 2; i <= n; i++) {
    if (a[i] > a[i - 1])
      cnt++;
    else
      cnt = 1;
    maxn = max(maxn, cnt);
  }
  cout << n - maxn << endl;
  return 0;
}
