#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 2e6 + 1;
const long long INF = 9e18;
const int M = 2010;
int a[N];
int pos[N];
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    pos[a[i]] = i;
  }
  int ans = 0;
  int cnt = 1;
  while (cnt <= n) {
    int cur = 1;
    while (cnt <= n && pos[cnt] <= pos[cnt + 1]) ++cur, ++cnt;
    ans = max(ans, cur);
    ++cnt;
  }
  cout << n - ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}
