#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int mxN = 1e6 + 5;

int n, a[mxN];

int dp[mxN][3];

int pref[mxN], cnt[mxN], now[mxN], lst[mxN], F[mxN];

int main() {
	ios :: sync_with_stdio(false), cin.tie(nullptr);
	cin >> n;
	for (int i = 1; i <= n; ++i) 
		cin >> a[i], ++cnt[a[i]];
	for (int i = n; i >= 1; i--)
		F[a[i]] = i;
	int ans = n;
	for (int i = 1; i <= n; ++i) {
		++now[a[i]];
        if (now[a[i]] == 1) {
        	dp[i][0] = pref[i - 1] + 1;
        	if (cnt[a[i]] == 1)
        		dp[i][2] = dp[i][0];
        }
        else if (now[a[i]] < cnt[a[i]]) {
        	if (lst[a[i]] == F[a[i]])
        		dp[i][1] = dp[lst[a[i]]][0] + 1;
        	else
        		dp[i][1] = dp[lst[a[i]]][1] + 1;
        }
        else {
        	if (lst[a[i]] == F[a[i]])
        		dp[i][2] = dp[lst[a[i]]][0] + 1;
        	else
        		dp[i][2] = dp[lst[a[i]]][1] + 1;
        }
    	pref[i] = max(pref[i - 1], dp[i][2]);
    	lst[a[i]] = i;
    }                                     
    int mx = 0;
    map <int, int> cur;
    for (int i = n; i >= 1; i--) {
   		ans = min(ans, n - (dp[i][2] + mx));
   		++cur[a[i]];
   		mx = max(mx, cur[a[i]]);
    }        
    cout << ans;
	return 0;
}