#include <bits/stdc++.h>

using namespace std;
typedef long long li;
const li MAX = (li)6e5 + 1;
const li inf = (li)1e8;
li n, m, dp[MAX], a[MAX], r[MAX], l[MAX], cnt[MAX];
map<li, li> am;
void solve(){
    cin >> n;
    for (int i = 1; i<= n; i++) {
        cin >> a[i];
        if (l[a[i]] == 0) l[a[i]] = i;
        r[a[i]] = i;
        cnt[a[i]]++;
    }
    for (int i = 1; i <= n; i++){
        dp[i] = dp[i - 1];
        if (r[a[i]] == i){
            dp[i] = max(dp[i], dp[l[a[i]] - 1] + cnt[a[i]]);
        }
    }
    multiset<li> vals;
    li ans = dp[n];
    for (int i = n; i >= 0; i--){
        if (vals.size())
        ans = max(ans, dp[i] + *(vals.rbegin()));
        else ans = max(ans, dp[i]);
        if (am[a[i]])
        vals.erase(vals.find(am[a[i]]));
        am[a[i]]++;
        vals.insert(am[a[i]]);
    }
    cout << n - ans;
}

int main()
{
    li q;
    q = 1;
    while(q--) solve();
    return 0;
}
