#include    <bits/stdc++.h>
#define fo(i,a,b) for(int i=(a);i<=(b);++i)
#define __unique(V) (V).resize(unique((V).begin(),(V).end())-(V).begin())
#define cntbit(X)   __builtin_popcount((X))
#define bit(S,i) (((S)>>(i))&1)
#define PI	acos(-1)
#define fi  first
#define se  second
#define LL  long long
#define ii  pair<int,int>
#define iii pair<int,ii>
#define eb  emplace_back
#define TASK "1481E"
using namespace std;
const int maxn = 5e5 + 5;
int n,a[maxn],dp[maxn],g[maxn],cnt[maxn],last[maxn],first[maxn];
///--------------------------
void    readf(){
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> a[i];
}
///--------------------------
void    solve(){
    for(int i = 1; i <= n; ++i) cnt[a[i]]++;
    for(int i = 1; i <= n; ++i) last[a[i]] = i;
    for(int i = n; i > 0; --i) first[a[i]] = i;
    for(int i = 1; i <= n; ++i) if (last[a[i]] == i){
        dp[i] = cnt[a[i]] + g[first[a[i]] - 1];
        g[i] = max(g[i - 1],dp[i]);
    }else g[i] = g[i - 1];
    int cuoi = 0,ans = 0;
    for(int i = 1; i <= n; ++i) cnt[i] = 0;
    for(int i = n; i > 0; --i) {
        cnt[a[i]]++;
        ans = max(ans,g[i - 1] + cnt[a[i]]);
    }
    cout << n - ans << '\n';
}
///--------------------------
int     main(){
	srand(time(NULL));
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    #ifdef TLH2203
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    #endif // TLH2203
    readf();
    solve();
}




