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