#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;

int mod = 998244353;

void solve(){
    int n; cin >> n;
    vector<int>A(n), dp(n+1), l(n+1), r(n+1), f(n+1),maxCnt(n+2);
    for (int i = 0 ;i < n; i++){
        cin >> A[i];
        if (!l[A[i]]) l[A[i]] = i+1;
        r[A[i]] = i+1;
    }
    for (int i = n - 1; i >= 0; i--){
        f[A[i]]++;
        maxCnt[i] = max(maxCnt[i+1], f[A[i]]);
    }
    int ans = n;
    for (int i = 1; i <= n; i++){
        dp[i] = dp[i-1];
        if (r[A[i-1]] == i){
            dp[i] = max(dp[i], dp[l[A[i-1]]] + f[A[i-1]]);
        }
        ans = min(ans, n - maxCnt[i] - dp[i]);
    }
    cout << ans << "\n";
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	ll t = 1;
	//cin >> t;
	while(t--){
		solve();
	}

	return 0;
}
//120 50 100 100 70000

