#include <bits/stdc++.h>
using namespace std;
int a[1000006], dp[1000006];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, x;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> x;
    dp[x] = i;
  }
  int curr = 0, best = -1;
  for (int i = 1; i <= N; i++) {
    if (dp[i] > dp[i - 1]) {
      curr++;
      best = max(best, curr);
    } else
      curr = 1;
  }
  cout << N - best << endl;
  return 0;
}
