#include <bits/stdc++.h>
using namespace std;
long long power(long long base, long long pow, long long mo) {
  long long res = 1;
  while (pow) {
    if (pow % 2 != 0) res = (res * base) % mo;
    base = (base * base) % mo;
    pow /= 2;
  }
  return res;
}
long long inv(long long i) { return power(i, 1000000007 - 2, 1000000007); }
std::string str = "All Good Till Here";
void solve() {
  long long i, j, x, n;
  cin >> n;
  long long a[n], p[n];
  for (long long c1 = 0; c1 < n; c1++) cin >> a[c1];
  ;
  for (long long i = 0; i < n; i++) {
    p[a[i] - 1] = i;
  }
  long long ans = 0, max2 = 1;
  for (long long i = 1; i < n; i++) {
    if (p[i] > p[i - 1]) {
      max2++;
    } else {
      ans = max(max2, ans);
      max2 = 1;
    }
  }
  ans = max(max2, ans);
  cout << n - ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long tt;
  tt = 1;
  while (tt--) {
    solve();
  }
  return 0;
}
