#include <iostream>
#include <cstdio>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 5e5+5;
int f[maxn] , sbest[maxn] , fir[maxn] , snum[maxn] , abest = 0;
int a[maxn] , id[maxn];
int main()
{
	int n; scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		int cnt = ++snum[ a[i] ];
		id[i] = cnt;
		if(id[i]==1) fir[ a[i] ] = i;
	}
	int ans = inf;
	for(int i=1;i<=n;++i)
	{
		f[i] = max( id[i] + sbest[ a[i] ] , abest + 1 );
		if( id[i] == snum[ a[i] ] ) abest = max( abest , f[ fir[ a[i] ] ] + snum[ a[i] ] - 1 );
		sbest[ a[i] ] = max( sbest[ a[i] ] , f[i] - id[i] );
		ans = min(ans,n-f[i]);
	}
	printf("%d",ans);
	return 0;
}