//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define endl "\n"
#define all(c) (c).begin(),(c).end()

// if you're reading this i hope to meet you in IOI 2021
// RECHECK CONSTRAINTS AFTER MAKING A REDUCTION

signed main() {
	// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n;
	cin >> n;
	vector<int> arr(n), cnt(n), lm(n, -1), rm(n, -1);
	for (int i = 0; i < n; i++)
		cin >> arr[i], arr[i]--, lm[arr[i]] = (lm[arr[i]] == -1 ? i : lm[arr[i]]), rm[arr[i]] = i+1, cnt[arr[i]]++;
	vector<int> mx(n+1);
	vector<vector<int>> rs(n+1);
	for (int i = 0; i < n; i++) if (rm[i] >= 0)
		rs[rm[i]].push_back(i);
	for (int r = 1; r <= n; r++) {
		mx[r] = mx[r-1];
		for (int e : rs[r])
			mx[r] = max(mx[r], cnt[e] + mx[lm[e]]);
	}
	map<int, int> m;
	int ans = mx[n], cur = 0;
	for (int i = n-1; i >= 0; i--)
		ans = max(ans, mx[i] + (cur = max(cur, ++m[arr[i]])));
	cout << n-ans << endl;
}
/* --- PSolving ---
 * Simplifying (getting rid of variables, conditions, code logic, etc.)
 * Reframing
 * Solving a subtask (subgoal, aux. problem, removing a condition or fixing a parameter, etc.)
 * Inducing
 * Divide and conquer
 * Working backwards
 * Visual intuition
 * !! Reductions don't have to be specializations, they can be generalizations
 */
