#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
vector<int> b;
int f[100100], a[100100];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) f[a[i]] = i;
  int i = 1;
  while (i <= n) {
    int k = 1;
    while (i + 1 <= n && f[i + 1] > f[i]) {
      i++;
      k++;
    }
    b.push_back(k);
    i++;
  }
  sort(b.begin(), b.end());
  int ans = 0;
  for (int i = 0; i < b.size() - 1; i++) ans += b[i];
  cout << ans << endl;
  return 0;
}
