#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 500005
int read(){
	int w=0,f=1;
	char c=' ';
	while(c<'0'||c>'9')c=getchar(),f=c=='-'?-1:f;
	while(c>='0'&&c<='9')w=w*10+c-48,c=getchar();
	return w*f;
}
int n,a[N],fi[N],la[N],cnt[N],maxn[N],ans,f[N];
signed main(){
	n=read(),ans=n-1;
	for(int i=1;i<=n;i++)
		a[i]=read();
	for(int i=n;i>0;i--){
		cnt[a[i]]++;
		maxn[i]=max(maxn[i+1],cnt[a[i]]);
		if(!la[a[i]])la[a[i]]=i;
		fi[a[i]]=i;
	}
	for(int i=1;i<=n;i++){
		f[i]=f[i-1];
		if(la[a[i]]==i)f[i]=max(f[i],f[fi[a[i]]-1]+cnt[a[i]]);
		ans=min(ans,n-maxn[i+1]-f[i]);
	}
	printf("%lld\n",ans);
	return 0;
}