#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
void inp(long long a[], long long n) {
  for (long long i = 0; i < n; i++) cin >> a[i];
}
void solve() {
  long long n;
  cin >> n;
  long long a[n];
  inp(a, n);
  long long b[n + 1];
  for (long long i = 0; i < n; i++) {
    b[a[i]] = i;
  }
  long long g = 1;
  vector<long long> v;
  for (long long i = 2; i < n + 1; i++) {
    if (b[i] > b[i - 1])
      g++;
    else {
      v.push_back(g);
      g = 1;
    }
  }
  v.push_back(g);
  sort((v).begin(), (v).end());
  cout << n - v[(long long)((v).size()) - 1];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
}
