#include <bits/stdc++.h>
using namespace std;
const long long maxn = (long long)1e5 + 9;
const long long mod = (long long)1e9 + 7;
long long a[maxn], b[maxn];
int main() {
  std::ios::sync_with_stdio(0);
  long long i, j, k, t, q, m, l, r, n;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]] = i;
  }
  long long val = 1e10, cnt = 1;
  for (i = 2; i <= n; i++) {
    if (b[i] < b[i - 1]) {
      val = ((val) < (n - cnt) ? (val) : (n - cnt));
      cnt = 1;
    } else
      cnt++;
  }
  val = ((val) < (n - cnt) ? (val) : (n - cnt));
  cout << val << '\n';
}
