#include "bits/stdc++.h"
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2")
using namespace std;
using ll = long long;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n; cin >> n;
	vector<int> a(n+1);
	vector<int> first(n+1), last(n+1), ct(n+1), cont(n+1), freq(n+1);
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		first[a[i]] = first[a[i]] == 0 ? i : first[a[i]];
		last[a[i]] = i;
		++ct[a[i]];
		cont[i] = 1 + (a[i] == a[i-1] ? cont[i-1] : 0);
	}
	vector<int> dp(n+1);
	set<array<int, 2>> st;
	vector<array<int, 4>> ar;
	for (int i = 1; i <= n; ++i) {
		if (first[i]) ar.push_back({first[i], last[i], i, ct[i]});
	}
	sort(begin(ar), end(ar));
	int mx = 0, ans = n;
	for (auto [l, r, id, c] : ar) {
		while (!st.empty()) {
			auto it = *st.begin();
			if (it[0] < l) {
				mx = max(mx, it[1]);
				st.erase(it);
			}
			else break;
		}
		dp[id] = mx + c;
		st.insert({r, dp[id]});
	}
	mx = 0;
	for (int i = 1; i <= n; ++i) {
		++freq[a[i]];
		ans = min(ans, n - mx - cont[i] - (ct[a[i]] - freq[a[i]]));
		// cout << ans << ' ';
		if (i == last[a[i]]) mx = max(mx, dp[a[i]]);
	}
	cout << ans;
}