#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &);
template <size_t n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  const tuple<T...> &) {}
template <size_t n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 const tuple<T...> &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T>
ostream &operator<<(ostream &os, const tuple<T...> &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  return os;
}
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
void fastios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
int const mod = 1000000007;
auto const inf = numeric_limits<int>::max() / 8;
signed main() {
  int n;
  while (cin >> n) {
    vector<int> pos(n), a(n);
    for (int i = 0; i < (int)(n); i++) cin >> a[i];
    for (int i = 0; i < (int)(n); i++) pos[a[i] - 1] = i;
    int l = 0;
    int inc = 1;
    ;
    while (l < n) {
      int r = l + 1;
      while (r < n && pos[r - 1] < pos[r]) r++;
      inc = max(inc, r - l);
      ;
      l = r;
    }
    cout << n - inc << endl;
  }
}
