#include <bits/stdc++.h>

using namespace std;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) ((int)((x).size()))
#define all(x) (x).begin(),(x).end()
#define deb(x) cerr << "(" << #x << " = " << x << ")\n";
#define bp(x) __builtin_popcount(x)

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;

#define ROOT 1,1,m
#define LS v+v,l,mid
#define RS v+v+1,mid+1,r

int n, m;
int a[100100];
int dl[100100], dr[100100];

int t[100100 * 4];

int get(int v, int l, int r, int L, int R) {
	if(r < L || R < l) return 0;
	if(L <= l && r <= R) return t[v];
	int mid = (l + r) >> 1;
	return max(get(LS, L, R), get(RS, L, R));
}

void update(int v, int l, int r, int pos, int val) {
	if(l == r) {
		t[v] = val;
		return;
	}
	int mid = (l + r) >> 1;
	if(pos <= mid)
		update(LS, pos, val);
	else
		update(RS, pos, val);
	t[v] = max(t[v+v], t[v+v+1]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for(int i = 0; i < n; ++i) {
    	int l, r;
    	cin >> l >> r;
    	a[l]++;
    	a[r+1]--;
    }
    for(int i = 1; i <= m; ++i) {
    	a[i] += a[i-1];
    }
    for(int i = 1; i <= m; ++i) {
    	dl[i] = get(ROOT, 1, a[i]) + 1;
    	update(ROOT, a[i], dl[i]);
    }
    memset(t, 0, sizeof t);
    for(int i = m; i >= 1; --i) {
    	dr[i] = get(ROOT, 1, a[i]) + 1;
    	update(ROOT, a[i], dr[i]);
    }
    int ans = 0;
    for(int i = 0; i <= m; ++i) {
    	ans = max(ans, dl[i] + dr[i+1]);
    }
    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    vector<int> arr(n);
    map<int,int> mp;
    for(int i=0;i<n;++i){
        cin>>arr[i];
        mp[arr[i]]++;
    }
    map<int,int> MP;
    for(auto x:mp)
        MP[x.second]++;
    for(int i=n-1;i>=0;--i){
        if(MP.size()==1){
            auto it = MP.begin();
            if((it->first)==1){
                cout<<i+1;
                return 0;
            }
        }
        else if(MP.size()==2){
            auto it = MP.begin();
            int f1 = it->first;
            ++it;
            int f2 = it->first;
            it = MP.begin();
            int s1 = it->second;
            ++it;
            int s2 = it->second;
            if(s1==1 || s2==1){
                if((f2-f1)==1 || (f1==1 && s1==1)){
                    cout<<i+1;
                    return 0;
                }
            }
        }
        int elem = arr[i];
        int cnt = mp[elem];
        --mp[elem];
        --MP[cnt];
        if(cnt!=1)
            MP[cnt-1]++;
        if(MP[cnt]==0)
            MP.erase(MP.find(cnt));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+11;
int dp[N][11];
int32_t main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;++i){
        int elem;
        cin>>elem;
        dp[i][elem]++;
    }
    for(int i=1;i<=n;++i){
        for(int j=1;j<=10;++j)
            dp[i][j] = dp[i][j] + dp[i-1][j];
    }
    for(int i=n;i>=1;--i){
        map<int,int> mp;
        for(int j=1;j<=10;++j){
            if(dp[i][j])
                mp[dp[i][j]]++;
        }
        if(mp.size()>2)
            continue;
        if(mp.size()==1){
            cout<<i;
            return 0;
        }
        auto it = mp.begin();
        int f1 = it->first;
        ++it;
        int f2 = it->first;
        it = mp.begin();
        int s1 = it->second;
        ++it;
        int s2 = it->second;
        if((f2-f1)==1){
            if(s1==1 || s2==1){
                cout<<i;
                return 0;
            }
        }
        if((f1==1 && s1==1) || (f2==1 && s2==1)){
            cout<<i;
            return 0;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    vector<int> arr(n);
    map<int,int> mp;
    for(int i=0;i<n;++i){
        cin>>arr[i];
        mp[arr[i]]++;
    }
    map<int,int> MP;
    for(auto x:mp)
        MP[x.second]++;
    for(int i=n-1;i>=0;--i){
        if(MP.size()==1){
            auto it = MP.begin();
            if((it->first)==1 || (it->second)==1){
                cout<<i+1;
                return 0;
            }
        }
        else if(MP.size()==2){
            auto it = MP.begin();
            int f1 = it->first;
            ++it;
            int f2 = it->first;
            it = MP.begin();
            int s1 = it->second;
            ++it;
            int s2 = it->second;
            if(s2==1){
                if((f2-f1)==1){
                    cout<<i+1;
                    return 0;
                }
            }
            if((f1==1 && s1==1)){
                cout<<i+1;
                return 0;
            }
        }
        int elem = arr[i];
        int cnt = mp[elem];
        --mp[elem];
        --MP[cnt];
        if(cnt!=1)
            MP[cnt-1]++;
        if(MP[cnt]==0)
            MP.erase(MP.find(cnt));
    }
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int n;
	cin>>n;
	int day[n+1];
	int tab[11][n+1];
	for(int v=1;v<11;v++)
		tab[v][0]=0;
	for(int h=1;h<=n;h++)
		cin>>day[h];
	for(int v=1;v<11;v++)
		for(int h=1;h<=n;h++)
		{
			tab[v][h]=tab[v][h-1];
			if(v == day[h]) tab[v][h]++;
		}

/*
	int coldiff[n+1];
	for(int h=0;h<=n;h++)
	{
		coldiff[h]=0;
		for(int v=1;v<11;v++)
			coldiff[h]+=abs(tab[v][h]-tab[v-1][h]);
	}
	int isone=0;
	int best=-1;
	for(int h=1;h<=n;h++)
	{
		if(coldiff[h] == 1)
			best=h;
		else
		{
*/
	int fival;
	int seval;
	int fico;
	int seco;
	int zeco;
	int colOK[n+1];
	for(int h=1;h<=n;h++)
	{
		fival=-1;
		seval=-1;
		fico=0;
		seco=0;
		zeco=0;
		colOK[h]=1;

		for(int v=1;v<11;v++)
		{
			if(tab[v][h] == 0)
				zeco++;
			else
			{
				if(fico == 0)
				{
					fico++;
					fival = tab[v][h];
				}
				else
				{
					if(tab[v][h] == fival)
						fico++;
					else
					{
						if(seco == 0)
						{
							seco++;
							seval = tab[v][h];
						}
						else
						{
							if(seval == tab[v][h])
								seco++;
							else
							{
								colOK[h] = -v;
							break;
							}}}}}}

//		cout<<"predeb:row:fival:fico:seval:seco:"<<h<<":"<<fival<<":"<<fico<<":"<<seval<<":"<<seco<<endl;
		if(colOK[h] != 1)
			continue;
		if( (fival == (seval+1)) && (fico == 1))
			continue;
		if( (seval == (fival+1)) && (seco == 1))
			continue;
		if( (fival == 1) && (fico == 1))
			continue;
		if( (seval == 1) && (seco == 1))
			continue;
		if( (fival == 1) && (seco == 0))
			continue;

		colOK[h] = 3;
	}

	int best = -1;
	for(int h=1;h<=n;h++)
	{
		if(colOK[h] == 1)
			best = h;
	}

	cout<<best<<endl;

	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int n;
	cin>>n;
	int day[n+1];
	int tab[11][n+1];
	for(int v=1;v<11;v++)
		tab[v][0]=0;
	for(int h=1;h<=n;h++)
		cin>>day[h];
	for(int v=1;v<11;v++)
		for(int h=1;h<=n;h++)
		{
			tab[v][h]=tab[v][h-1];
			if(v == day[h]) tab[v][h]++;
		}

/*
	int coldiff[n+1];
	for(int h=0;h<=n;h++)
	{
		coldiff[h]=0;
		for(int v=1;v<11;v++)
			coldiff[h]+=abs(tab[v][h]-tab[v-1][h]);
	}
	int isone=0;
	int best=-1;
	for(int h=1;h<=n;h++)
	{
		if(coldiff[h] == 1)
			best=h;
		else
		{
*/
	int fival;
	int seval;
	int fico;
	int seco;
	int zeco;
	int colOK[n+1];
	for(int h=1;h<=n;h++)
	{
		fival=-1;
		seval=-1;
		fico=0;
		seco=0;
		zeco=0;
		colOK[h]=1;

		for(int v=1;v<11;v++)
		{
			if(tab[v][h] == 0)
				zeco++;
			else
			{
				if(fico == 0)
				{
					fico++;
					fival = tab[v][h];
				}
				else
				{
					if(tab[v][h] == fival)
						fico++;
					else
					{
						if(seco == 0)
						{
							seco++;
							seval = tab[v][h];
						}
						else
						{
							if(seval == tab[v][h])
								seco++;
							else
							{
								colOK[h] = -v;
							break;
							}}}}}}

//		cout<<"predeb:row:fival:fico:seval:seco:"<<h<<":"<<fival<<":"<<fico<<":"<<seval<<":"<<seco<<endl;
		if(colOK[h] != 1)
			continue;
		if( (fival == (seval+1)) && (fico == 1))
			continue;
		if( (seval == (fival+1)) && (seco == 1))
			continue;
		if( (fival == 1) && (fico == 1))
			continue;
		if( (seval == 1) && (seco == 1))
			continue;
		if(  (fival == 1) && (seco == 0) )
			continue;
		if( (fico == 1) && (seco ==0))
		     continue;

		colOK[h] = 3;
	}

	int best = -1;
	for(int h=1;h<=n;h++)
	{
		if(colOK[h] == 1)
			best = h;
	}

	cout<<best<<endl;

	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int n;
	int lastcol=0; //biggest index of nonzero color
	cin>>n;
	int day[n+1];
//	int tab[11][n+1];
	int tab[100001];
	for(int v=1;v<100001;v++)
		tab[v]=0;
	for(int h=1;h<=n;h++)
		cin>>day[h];
/*	for(int v=1;v<11;v++)
		for(int h=1;h<=n;h++)
		{
			tab[v][h]=tab[v][h-1];
			if(v == day[h]) tab[v][h]++;
		}
*/
	int fival;
	int seval;
	int fico;
	int seco;
	int zeco;
	int colOK[n+1];

	for(int h=1;h<=n;h++)
	{
		tab[day[h]]++;
		lastcol = max(lastcol, day[h]);
		fival = -1;
		seval = -1;
		fico  = 0;
		seco  = 0;
		zeco  = 0;
		colOK[h] = 1;

		for(int v=1;v<=lastcol;v++)
		{
			if(tab[v] == 0)
			{
				zeco++;
			}
			else if(fico == 0)
			{
				fico++;
				fival = tab[v];
			}
			else if(tab[v] == fival)
			{
				fico++;
			}
			else if(seco == 0)
			{
				seco++;
				seval = tab[v];
			}
			else if(seval == tab[v])
			{
				seco++;
			}
			else
			{
				colOK[h] = -1;
				break;
			}
		}
		  if(colOK[h] != 1)
			{
				continue;
			}
		  if( (fival == (seval + 1)) && (fico == 1))
			{
				continue;
			}
			if( (seval == (fival + 1)) && (seco == 1))
			{
				continue;
			}
			if( (fival == 1) && (fico == 1))
			{
				continue;
			}
			if( (seval == 1) && (seco == 1))
			{
				continue;
			}
			if( (fival == 1) && (seco == 0))
			{
				continue;
			}
			if( (fico == 1) && (seco ==0))
		     continue;

			colOK[h] = 3;
	}

	int best = 0;
	for(int h=1;h<=n;h++)
	{
		if(colOK[h] == 1)
		{
			best = h;
		}
	}

		cout<<best<<endl;

	return 0;
}


#include<iostream>
using namespace std;

int nonzerolist[100001];
int tab[100001];

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int nonzeroco=0;

	int n;
	int lastcol=0; //biggest index of nonzero color
	cin>>n;
	int day[n+1];
//	int tab[11][n+1];
	for(int v=1;v<100001;v++)
	{
		tab[v]=0;
	}
/*	for(int v=1;v<11;v++)
		for(int h=1;h<=n;h++)
		{
			tab[v][h]=tab[v][h-1];
			if(v == day[h]) tab[v][h]++;
		}
*/
	int maxnonzero = 0;
	int fival;
	int seval;
	int fico;
	int seco;
	int zeco;
	int colOK[n+1];

	for(int h=1;h<=n;h++)
	{
		cin>>day[h];
		maxnonzero = max(maxnonzero, ++tab[day[h]]);
		if(tab[day[h]] == 1)
		{
			nonzerolist[nonzeroco++] = day[h];
		}
		if( ( (nonzeroco != 1) && ( (h % nonzeroco) != 1) && (maxnonzero > 1)))
		{
			colOK[h] = 2;
			continue;
		}
		fival = -1;
		seval = -1;
		fico  = 0;
		seco  = 0;
		zeco  = 0;
		colOK[h] = 1;

		for(int v=0;v<nonzeroco;v++)
		{
			if(tab[nonzerolist[v]] == 0)
			{
				zeco++;
			}
			else if(fico == 0)
			{
				fico++;
				fival = tab[nonzerolist[v]];
			}
			else if(tab[nonzerolist[v]] == fival)
			{
				fico++;
			}
			else if(seco == 0)
			{
				seco++;
				seval = tab[nonzerolist[v]];
			}
			else if(seval == tab[nonzerolist[v]])
			{
				seco++;
			}
			else
			{
				colOK[h] = -1;
				break;
			}
		}
		  if(colOK[h] != 1)
			{
				continue;
			}
		  if( (fival == (seval + 1)) && (fico == 1))
			{
				continue;
			}
			if( (seval == (fival + 1)) && (seco == 1))
			{
				continue;
			}
			if( (fival == 1) && (fico == 1))
			{
				continue;
			}
			if( (seval == 1) && (seco == 1))
			{
				continue;
			}
			if( (fival == 1) && (seco == 0))
			{
				continue;
			}
			if( (fico == 1) && (seco ==0))
			{
				continue;
			}
			colOK[h] = 3;
	}

	int best = 0;
	for(int h=1;h<=n;h++)
	{
		if(colOK[h] == 1)
		{
			best = h;
		}
	}

		cout<<best<<endl;

	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int n;
	int lastcol=0; //biggest index of nonzero color
	cin>>n;
	int day[n+1];
//	int tab[11][n+1];
	int tab[100001];
	for(int v=1;v<100001;v++)
		tab[v]=0;
	for(int h=1;h<=n;h++)
		cin>>day[h];
/*	for(int v=1;v<11;v++)
		for(int h=1;h<=n;h++)
		{
			tab[v][h]=tab[v][h-1];
			if(v == day[h]) tab[v][h]++;
		}
*/
	int fival;
	int seval;
	int fico;
	int seco;
	int zeco;
	int colOK[n+1];

	for(int h=1;h<=n;h++)
	{
		tab[day[h]]++;
		lastcol = max(lastcol, day[h]);
		fival = -1;
		seval = -1;
		fico  = 0;
		seco  = 0;
		zeco  = 0;
		colOK[h] = 1;

		for(int v=1;v<=lastcol;v++)
		{
			if(tab[v] == 0)
			{
				zeco++;
			}
			else if(fico == 0)
			{
				fico++;
				fival = tab[v];
			}
			else if(tab[v] == fival)
			{
				fico++;
			}
			else if(seco == 0)
			{
				seco++;
				seval = tab[v];
			}
			else if(seval == tab[v])
			{
				seco++;
			}
			else
			{
				colOK[h] = -1;
				break;
			}
		}
		  if(colOK[h] != 1)
			{
				continue;
			}
		  if( (fival == (seval + 1)) && (fico == 1))
			{
				continue;
			}
			if( (seval == (fival + 1)) && (seco == 1))
			{
				continue;
			}
			if( (fival == 1) && (fico == 1))
			{
				continue;
			}
			if( (seval == 1) && (seco == 1))
			{
				continue;
			}
			if( (fival == 1) && (seco == 0))
			{
				continue;
			}

			colOK[h] = 3;
	}

	int best = 0;
	for(int h=1;h<=n;h++)
	{
		if(colOK[h] == 1)
		{
			best = h;
		}
	}

		cout<<best<<endl;

	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int n;
	cin>>n;
	int day[n+1];
	int tab[11][n+1];
	for(int v=1;v<11;v++)
		tab[v][0]=0;
	for(int h=1;h<=n;h++)
		cin>>day[h];
	for(int v=1;v<11;v++)
		for(int h=1;h<=n;h++)
		{
			tab[v][h]=tab[v][h-1];
			if(v == day[h]) tab[v][h]++;
		}

/*
	int coldiff[n+1];
	for(int h=0;h<=n;h++)
	{
		coldiff[h]=0;
		for(int v=1;v<11;v++)
			coldiff[h]+=abs(tab[v][h]-tab[v-1][h]);
	}
	int isone=0;
	int best=-1;
	for(int h=1;h<=n;h++)
	{
		if(coldiff[h] == 1)
			best=h;
		else
		{
*/
	int fival;
	int seval;
	int fico;
	int seco;
	int zeco;
	int colOK[n+1];
	for(int h=1;h<=n;h++)
	{
		fival=-1;
		seval=-1;
		fico=0;
		seco=0;
		zeco=0;
		colOK[h]=1;

		for(int v=1;v<11;v++)
		{
			if(tab[v][h] == 0)
				zeco++;
			else
			{
				if(fico == 0)
				{
					fico++;
					fival = tab[v][h];
				}
				else
				{
					if(tab[v][h] == fival)
						fico++;
					else
					{
						if(seco == 0)
						{
							seco++;
							seval = tab[v][h];
						}
						else
						{
							if(seval == tab[v][h])
								seco++;
							else
							{
								colOK[h] = -v;
							break;
							}}}}}}

//		cout<<"predeb:row:fival:fico:seval:seco:"<<h<<":"<<fival<<":"<<fico<<":"<<seval<<":"<<seco<<endl;
		if(colOK[h] != 1)
			continue;
		if( (fival == (seval+1)) && (fico == 1))
			continue;
		if( (seval == (fival+1)) && (seco == 1))
			continue;
		if( (fival == 1) && (fico == 1))
			continue;
		if( (seval == 1) && (seco == 1))
			continue;
		if(  seco == 0 )
			continue;

		colOK[h] = 3;
	}

	int best = -1;
	for(int h=1;h<=n;h++)
	{
		if(colOK[h] == 1)
			best = h;
	}

	cout<<best<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
set<int> s[100001];
int cr[100001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,arr[100001];
	cin>>n;
	FOR(0,n,1)
		cin>>arr[i],cr[arr[i]]++,s[cr[arr[i]]].insert(arr[i]),s[cr[arr[i]]-1].erase(arr[i]);
	int c=0,ind;
	rep(1,n,1)
		if(s[j].size()>0)
			ind=j,c++;
	ll p=-1,q=-1,z=0;
	ll st = n-1;
	if(c==1)
	{
		if(ind==1 || s[ind].size()==1)
		{
			cout<<n;
			return 0;
		}
	}
	repp(st,0,1)
	{
		if(c==2)
		{
			if(!z)
			{
				int k=0;
				FOR(1,n+1,1)
					if(s[i].size()>0)
						if(k)
							q=i;
						else
							p=i,k=1;
				z=1;
			}
			if(z)
			{
				if( (q-p==1 && s[q].size()==1) || (p==1 && s[p].size()==1)  )
				{
					cout<<j+1;
					return 0;
				}
				else
				{
					int k = cr[arr[j]];
					if(p==k && s[k].size()==1)
						p--;
					if(q==k && s[k].size()==1)
						q--;
					if(p>q)
						swap(p,q);
					cr[arr[j]]--;
					s[k].erase(arr[j]);
					if(s[k-1].size()==0 && k>1)
						c++;
					s[k-1].insert(arr[j]);
					if(s[k].size()==0)
						c--;
				}
			}
		}
		else
		{
			int k = cr[arr[j]];
			cr[arr[j]]--;
			s[k].erase(arr[j]);
			if(s[k-1].size()==0 && k>1)
				c++;
			s[k-1].insert(arr[j]);
			if(s[k].size()==0)
				c--;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
set<int> s[100001];
int cr[100001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,arr[100001];
	cin>>n;
	FOR(0,n,1)
		cin>>arr[i],cr[arr[i]]++,s[cr[arr[i]]].insert(arr[i]),s[cr[arr[i]]-1].erase(arr[i]);
	int c=0,ind;
	rep(1,n,1)
		if(s[j].size()>0)
			ind=j,c++;
	ll p=-1,q=-1,z=0;
	ll st = n-1;
	if(c==1)
	{
		if(ind==1 || s[ind].size()==1)
		{
			cout<<n;
			return 0;
		}
		else
			s[cr[arr[n-1]]].erase(arr[n-1]),s[cr[arr[n-1]]-1].insert(arr[n-1]),cr[arr[n-1]]--,c=2,st=n-2,q=cr[arr[n-1]]+1,p=q-1,z=1;
	}
	repp(st,0,1)
	{
		if(c==2)
		{
			if(!z)
			{
				int k=0;
				FOR(1,n+1,1)
					if(s[i].size()>0)
						if(k)
							q=i;
						else
							p=i,k=1;

				z=1;
			}
			if(z)
			{
				if( (q-p==1 && s[q].size()==1) || (p==1 && s[p].size()==1) || (q==p && s[p].size()==1 && s[q].size()==1) )
				{
					cout<<j+1;
					return 0;
				}
				else
				{
					int k = cr[arr[j]];
					if(p==k && s[k].size()==1)
						p--;
					if(q==k && s[k].size()==1)
						q--;
					if(p>q)
						swap(p,q);
					cr[arr[j]]--;
					s[k].erase(arr[j]);
					if(s[k-1].size()==0 && k>1)
						c++;
					s[k-1].insert(arr[j]);
					if(s[k].size()==0)
						c--;
				}
			}
		}
		else
		{
			int k = cr[arr[j]];
			cr[arr[j]]--;
			s[k].erase(arr[j]);
			if(s[k-1].size()==0 && k>1)
				c++;
			s[k-1].insert(arr[j]);
			if(s[k].size()==0)
				c--;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
set<int> s[100001];
int cr[100001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,arr[100001];
	cin>>n;
	FOR(0,n,1)
		cin>>arr[i],cr[arr[i]]++,s[cr[arr[i]]].insert(arr[i]),s[cr[arr[i]]-1].erase(arr[i]);
	int c=0,ind;

	rep(1,n,1)
		if(s[j].size()>0)
			ind=j,c++;

	// c gives count of no. of different frequencies
	ll p=-1,q=-1,z=0;
	ll st = n-1;
	if(c==1)
	{
		// For cases like 1 2 3 4 5 and  5 5 5 5 5
		if(ind==1 || s[ind].size()==1)
		{
			cout<<n;
			return 0;
		}
	}
	repp(st,0,1)
	{
		if(c==2)
		{
			if(!z)
			{
				int k=0;
				FOR(1,n+1,1)
					if(s[i].size()>0)
						if(k)
							q=i;
						else
							p=i,k=1;
				z=1;
			}
			if(z)
			{
				// Cases like 2 2 3 4 3 4 4 and 5 5 4 4 3 3 2
				if(n==99999 && arr[0]==5)
				{
					cout<<q<<" "<<p<<" "<<s[q].size()<<" "<<s[p].size()<<" "<<j<<endl;
				}
				if( (q-p==1 && s[q].size()==1) || (p==1 && s[p].size()==1)  )
				{
					cout<<j+1;
					return 0;
				}
				else
				{
					int k = cr[arr[j]];
					if(p==k && s[k].size()==1)
						p--;
					if(q==k && s[k].size()==1)
						q--;
					if(p>q)
						swap(p,q);
					cr[arr[j]]--;
					s[k].erase(arr[j]);
					if(s[k-1].size()==0 && k>1)
						c++;
					s[k-1].insert(arr[j]);
					if(s[k].size()==0)
						c--;
				}
			}
		}
		else
		{
			int k = cr[arr[j]];
			cr[arr[j]]--;
			s[k].erase(arr[j]);
			if(s[k-1].size()==0 && k>1)
				c++;
			s[k-1].insert(arr[j]);
			if(s[k].size()==0)
				c--;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
set<int> s[100001];
int cr[100001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,arr[100001];
	cin>>n;
	FOR(0,n,1)
		cin>>arr[i],cr[arr[i]]++,s[cr[arr[i]]].insert(arr[i]),s[cr[arr[i]]-1].erase(arr[i]);
	int c=0,ind;
	rep(1,n,1)
		if(s[j].size()>0)
			ind=j,c++;
	ll p=-1,q=-1,z=0;
	ll st = n-1;
	if(c==1)
	{
		if(ind==1 || s[ind].size()==1)
		{
			cout<<n;
			return 0;
		}
		else
			s[cr[arr[n-1]]].erase(arr[n-1]),s[cr[arr[n-1]]-1].insert(arr[n-1]),cr[arr[n-1]]--,c=2,st=n-2,q=cr[arr[n-1]]+1,p=q-1,z=1;
	}
	repp(st,0,1)
	{
		if(c==2)
		{
			if(!z)
			{
				int k=0;
				FOR(1,n+1,1)
					if(s[i].size()>0)
						if(k)
							q=i;
						else
							p=i,k=1;

				z=1;
			}
			if(z)
			{
				if( (q-p==1 && s[q].size()==1) || (p==1 && s[p].size()==1) )
				{
					cout<<j+1;
					return 0;
				}
				else
				{
					int k = cr[arr[j]];
					if(p==k && s[k].size()==1)
						p--;
					if(q==k && s[k].size()==1)
						q--;
					cr[arr[j]]--;
					s[k].erase(arr[j]);
					if(s[k-1].size()==0 && k>1)
						c++;
					s[k-1].insert(arr[j]);
					if(s[k].size()==0)
						c--;
				}
			}
		}
		else
		{
			int k = cr[arr[j]];
			cr[arr[j]]--;
			s[k].erase(arr[j]);
			if(s[k-1].size()==0 && k>1)
				c++;
			s[k-1].insert(arr[j]);
			if(s[k].size()==0)
				c--;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
set<int> s[100001];
int cr[100001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,arr[100001];
	cin>>n;
	FOR(0,n,1)
		cin>>arr[i],cr[arr[i]]++,s[cr[arr[i]]].insert(arr[i]),s[cr[arr[i]]-1].erase(arr[i]);
	int c=0,ind;

	rep(1,n,1)
		if(s[j].size()>0)
			ind=j,c++;

	// c gives count of no. of different frequencies
	ll p=-1,q=-1,z=0;
	ll st = n-1;
	if(c==1)
	{
		// For cases like 1 2 3 4 5 and  5 5 5 5 5
		if(ind==1 || s[ind].size()==1)
		{
			cout<<n;
			return 0;
		}
	}
	repp(st,0,1)
	{
		if(c==2)
		{
			if(!z)
			{
				int k=0;
				FOR(1,n+1,1)
					if(s[i].size()>0)
						if(k)
							q=i;
						else
							p=i,k=1;
				z=1;
			}
			if(z)
			{
				// Cases like 2 2 3 4 3 4 4 and 5 5 4 4 3 3 2
				if( (q-p==1 && s[q].size()==1) || (p==1 && s[p].size()==1)  )
				{
					cout<<j+1;
					return 0;
				}
				else
				{
					int k = cr[arr[j]];
					if(p==k && s[k].size()==1)
						p--;
					if(q==k && s[k].size()==1)
						q--;
					if(p>q)
						swap(p,q);
					cr[arr[j]]--;
					s[k].erase(arr[j]);
					if(s[k-1].size()==0 && k>1)
						c++;
					s[k-1].insert(arr[j]);
					if(s[k].size()==0)
						c--;
				}
			}
		}
		else
		{
			z=0;
			int k = cr[arr[j]];
			cr[arr[j]]--;
			s[k].erase(arr[j]);
			if(s[k-1].size()==0 && k>1)
				c++;
			s[k-1].insert(arr[j]);
			if(s[k].size()==0)
				c--;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
set<int> s[100001];
int cr[100001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,arr[100001];
	cin>>n;
	FOR(0,n,1)
		cin>>arr[i],cr[arr[i]]++,s[cr[arr[i]]].insert(arr[i]),s[cr[arr[i]]-1].erase(arr[i]);
	int c=0,ind;

	rep(1,n,1)
		if(s[j].size()>0)
			ind=j,c++;

	// c gives count of no. of different frequencies
	ll p=-1,q=-1,z=0;
	ll st = n-1;
	if(c==1)
	{
		// For cases like 1 2 3 4 5 and  5 5 5 5 5
		if(ind==1 || s[ind].size()==1)
		{
			cout<<n;
			return 0;
		}
	}
	repp(st,0,1)
	{
		if(c==2)
		{
			if(!z)
			{
				int k=0;
				FOR(1,n+1,1)
					if(s[i].size()>0)
						if(k)
							q=i;
						else
							p=i,k=1;
				z=1;
			}
			if(z)
			{
				// Cases like 2 2 3 4 3 4 4 and 5 5 4 4 3 3 2
				if(n==99999 && arr[0]==5)
				{
					cout<<q<<" "<<p<<" "<<s[q].size()<<" "<<s[p].size()<<endl;
				}
				if( (q-p==1 && s[q].size()==1) || (p==1 && s[p].size()==1)  )
				{
					cout<<j+1;
					return 0;
				}
				else
				{
					int k = cr[arr[j]];
					if(p==k && s[k].size()==1)
						p--;
					if(q==k && s[k].size()==1)
						q--;
					if(p>q)
						swap(p,q);
					cr[arr[j]]--;
					s[k].erase(arr[j]);
					if(s[k-1].size()==0 && k>1)
						c++;
					s[k-1].insert(arr[j]);
					if(s[k].size()==0)
						c--;
				}
			}
		}
		else
		{
			int k = cr[arr[j]];
			cr[arr[j]]--;
			s[k].erase(arr[j]);
			if(s[k-1].size()==0 && k>1)
				c++;
			s[k-1].insert(arr[j]);
			if(s[k].size()==0)
				c--;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
ll cols[100001][11];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,ans;
	cin>>n;
	ll arr[100001];
	FOR(0,n,1)
		cin>>arr[i];
	int l=0,r=n;
	if(n==1)
	{
		cout<<1;
		return 0;
	}
	FOR(0,n,1)
	{
		if(i==0)
			cols[i][arr[i]]++;
		else
		{
			rep(1,10,1)
				cols[i][j]=cols[i-1][j];
			cols[i][arr[i]]++;
			vector<int> gp;
			rep(1,10,1)
			{
				if(cols[i][j]>0)
					gp.pb(cols[i][j]);
			}
			sort(gp.begin(),gp.end());
			int z = 0,p=-1,oc=0;
			FOR(0,gp.size(),1)
			{
				if(gp[i]!=p)
					z++,p=gp[i];
				if(gp[i]==1)
					oc++;
			}
			if(z>2)
				continue;
			else
			{
				vector<int> v;
				int p=-1;
				FOR(0,gp.size(),1)
				{
					if(gp[i]!=p)
						v.pb(gp[i]),p=gp[i];
				}
				sort(v.begin(),v.end());
				if(v.size()==1)
				{
					if(v[0]==1)
					{
						ans=i+1;
						continue;
					}
				}
				if(v[1]-v[0]==1 || (v[0]==1 && (oc==1 || oc==2)))
				{
					ans=i+1;}
			}
		}
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
set<int> s[100001];
int cr[100001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,arr[100001];
	cin>>n;
	FOR(0,n,1)
		cin>>arr[i],cr[arr[i]]++,s[cr[arr[i]]].insert(arr[i]),s[cr[arr[i]]-1].erase(arr[i]);
	int c=0,ind;
	rep(1,n,1)
		if(s[j].size()>0)
			ind=j,c++;
	ll p=-1,q=-1,z=0;
	ll st = n-1;
	if(c==1)
	{
		if(ind==1 || s[ind].size()==1)
		{
			cout<<n;
			return 0;
		}
		else
			s[cr[arr[n-1]]].erase(arr[n-1]),s[cr[arr[n-1]]-1].insert(arr[n-1]),cr[arr[n-1]]--,c=2,st=n-2,q=cr[arr[n-1]]+1,p=q-1,z=1;
	}
	repp(st,0,1)
	{
		if(c==2)
		{
			if(!z)
			{
				int k=0;
				FOR(1,n+1,1)
					if(s[i].size()>0)
						if(k)
							q=i;
						else
							p=i,k=1;

				z=1;
			}
			if(z)
			{
				if( (q-p==1 && s[q].size()==1) || (p==1 && s[p].size()==1) || ( (q==p && (s[p].size()==1 || s[q].size()==1)) ) )
				{
					cout<<j+1;
					return 0;
				}
				else
				{
					int k = cr[arr[j]];
					if(p==k && s[k].size()==1)
						p--;
					if(q==k && s[k].size()==1)
						q--;
					if(p>q)
						swap(p,q);
					cr[arr[j]]--;
					s[k].erase(arr[j]);
					if(s[k-1].size()==0 && k>1)
						c++;
					s[k-1].insert(arr[j]);
					if(s[k].size()==0)
						c--;
				}
			}
		}
		else
		{
			int k = cr[arr[j]];
			cr[arr[j]]--;
			s[k].erase(arr[j]);
			if(s[k-1].size()==0 && k>1)
				c++;
			s[k-1].insert(arr[j]);
			if(s[k].size()==0)
				c--;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(long long i=I;i<A;i+=B)
#define rep(I,A,B) for(long long j=I;j<=A;j+=B)
#define repp(I,A,B) for(long long j=I;j>=A;j-=B)
#define str(A) for(long long k=0;A[k]!='\0';k++)
#define pb push_back
#define mp make_pair
#define pii pair<long long,long long>
vector <int> adj[100001];
bool visit[100001];
const int mod = 1000000007;
set<int> s[100001];
int cr[100001];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n,i,arr[100001];
	cin>>n;
	FOR(0,n,1)
		cin>>arr[i],cr[arr[i]]++,s[cr[arr[i]]].insert(arr[i]),s[cr[arr[i]]-1].erase(arr[i]);
	int c=0,ind;
	rep(1,n,1)
		if(s[j].size()>0)
			ind=j,c++;
	ll p=-1,q=-1,z=0;
	ll st = n-1;
	if(c==1)
	{
		if(ind==1 || s[ind].size()==1)
		{
			cout<<n;
			return 0;
		}
		else
			s[cr[arr[n-1]]].erase(arr[n-1]),s[cr[arr[n-1]]-1].insert(arr[n-1]),cr[arr[n-1]]--,c=2,st=n-2,q=cr[arr[n-1]]+1,p=q-1,z=1;
	}
	repp(st,0,1)
	{
		if(c==2)
		{
			if(!z)
			{
				int k=0;
				FOR(1,n+1,1)
					if(s[i].size()>0)
						if(k)
							q=i;
						else
							p=i,k=1;

				z=1;
			}
			if(z)
			{
				if( (q-p==1 && s[q].size()==1) || (p==1 && s[p].size()==1) )
				{
					cout<<j+1;
					return 0;
				}
				else
				{
					int k = cr[arr[j]];
					if(p==k)
						p--;
					if(q==k)
						q--;
					cr[arr[j]]--;
					s[k].erase(arr[j]);
					if(s[k-1].size()==0 && k>1)
						c++;
					s[k-1].insert(arr[j]);
					if(s[k].size()==0)
						c--;
				}
			}
		}
		else
		{
			int k = cr[arr[j]];
			cr[arr[j]]--;
			s[k].erase(arr[j]);
			if(s[k-1].size()==0 && k>1)
				c++;
			s[k-1].insert(arr[j]);
			if(s[k].size()==0)
				c--;
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#include<stdio.h>
#include<math.h>
#define pb push_back
#define mp make_pair
#define endl '\n'
#define lli long long int
#define li long int
#define FOR(i,n) for (li i = 0; i < n; i++)
#define loop(i,a,b) for (li i = a; i = 0; i--)
#define loopD(i,a,b) for (li i = a; i >= b; i--)
#define vi vector<int>
#define vl vector

#define vll vector<lli>
#define CLEAR(a)  memset(a,0,sizeof(a))
#define CLEARN(a) memset(a,-1,sizeof(a))
#define F first
#define S second
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define mii map<int,int>
#define mll map

#define pii pair<int,int>
#define pll pair

#define all(v) v.begin(),v.end()
#define pq priority_queue

#define spq priority_queue
#define pql priority_queue<pair
>
#define pqs priority_queue<pair
,vector< pair
 >,greater<pair
>>
#define lb(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()
long int mod=1e9+7;
#define PI 3.1415926535897
li i,j,n,m,k,l,t,fre[100050][10];
 mll ank;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    loop(i,1,n+1)
    {
        cin>>j;
        ank[j]++;
        fre[i][j]=1;
        loop(k,1,11)
            fre[i][k]+=fre[i-1][k];
    }
    if(ank.size()==1)
       {cout<<n; return 0;}
    for(i=n; i>0 ; i--)
    {
        set
ans;

         for(j=1 ; j<=10 ; j++)
         if(fre[i][j])
         ans.insert(fre[i][j]);
         vl rag;
         for(auto k:ans)
         rag.pb(k);
         if(ans.size()==1 && rag[0]==1)
         {cout<<i; return 0;}
         else if(ans.size()==2)
         {
             li flag=0,bha=0;
              for(j=1 ; j<=10 ; j++)
              if(fre[i][j]){
              if(fre[i][j]==rag[0])
              flag++;
                  else if(fre[i][j]==rag[1])
                  bha++;
              }
              if(flag==1)
              {
                  if(rag[0]==1)
                    {cout<<i; return 0;}
                  if(rag[0]-1==rag[1])
                   {cout<<i; return 0;}
              }
               if(bha==1)
              {
                   if(rag[1]==1)
                    {cout<<i; return 0;}
                  if(rag[1]-1==rag[0])
                   {cout<<i; return 0;}
              }

         }
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#include<stdio.h>
#include<math.h>
#define pb push_back
#define mp make_pair
#define endl '\n'
#define lli long long int
#define li long int
#define FOR(i,n) for (li i = 0; i < n; i++)
#define loop(i,a,b) for (li i = a; i = 0; i--)
#define loopD(i,a,b) for (li i = a; i >= b; i--)
#define vi vector<int>
#define vl vector

#define vll vector<lli>
#define CLEAR(a)  memset(a,0,sizeof(a))
#define CLEARN(a) memset(a,-1,sizeof(a))
#define F first
#define S second
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define mii map<int,int>
#define mll map

#define pii pair<int,int>
#define pll pair

#define all(v) v.begin(),v.end()
#define pq priority_queue

#define spq priority_queue
#define pql priority_queue<pair
>
#define pqs priority_queue<pair
,vector< pair
 >,greater<pair
>>
#define lb(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()
long int mod=1e9+7;
#define PI 3.1415926535897
li i,j,n,m,k,l,t,vis[100500];
set
ans;
mll a;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    loop(i,0,n)
    {
        cin>>j;
        ans.insert(j);
        if(!vis[j]){
        vis[j]++;
        a[vis[j]]++;}
        else
        {
            a[vis[j]]--;
            if(!a[vis[j]])
             a.erase(vis[j]);
              vis[j]++;
              a[vis[j]]++;
        }
        //  cout<<i<<" "<<a.size()<<endl;
        //  for(auto k:a)
        //  cout<<k.F<<" "<<k.S<<endl;
        //  cout<<endl;
        if((a.size()==1 && ans.size()==1) || (a.size()==1 && a.begin()->F==1))
        l=max(l,i+1);
        else if(a.size()==2)
        {
            vl rag,bha;
            for(auto k:a){
            rag.pb(k.S);
            bha.pb(k.F);}
            if(rag[0]==1)
            {
               if(bha[0]==1)
                 l=max(l,i+1);
                 if(bha[0]-1==bha[1])
                   l=max(l,i+1);
            }
            if(rag[1]==1)
            {
                if(bha[1]==1)
                  l=max(l,i+1);
                   if(bha[1]-1==bha[0])
                     l=max(l,i+1);
            }
        }
    }
    cout<<l;
    return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#include<stdio.h>
#include<math.h>
#define pb push_back
#define mp make_pair
#define endl '\n'
#define lli long long int
#define li long int
#define FOR(i,n) for (li i = 0; i < n; i++)
#define loop(i,a,b) for (li i = a; i = 0; i--)
#define loopD(i,a,b) for (li i = a; i >= b; i--)
#define vi vector<int>
#define vl vector

#define vll vector<lli>
#define CLEAR(a)  memset(a,0,sizeof(a))
#define CLEARN(a) memset(a,-1,sizeof(a))
#define F first
#define S second
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define mii map<int,int>
#define mll map

#define pii pair<int,int>
#define pll pair

#define all(v) v.begin(),v.end()
#define pq priority_queue

#define spq priority_queue
#define pql priority_queue<pair
>
#define pqs priority_queue<pair
,vector< pair
 >,greater<pair
>>
#define lb(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()
long int mod=1e9+7;
#define PI 3.1415926535897
li i,j,n,m,k,l,t,fre[100050][10];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    loop(i,1,n+1)
    {
        cin>>j;
        fre[i][j]=1;
        loop(k,1,11)
            fre[i][k]+=fre[i-1][k];
    }
    //   loop(i,1,n+1){
    //     loop(k,1,11)
    //     cout<<fre[i][k]<<" ";
    //     cout<<endl;}
    //     cout<<endl;
    for(i=n; i>0 ; i--)
    {
        set
a;
        for(j=1 ; j<=10 ; j++){
      //      cout<<j<<" ";
        if(fre[i][j])
        a.insert(fre[i][j]);}
        //  for(auto k:a)
        //  cout<<"ans "<<k<<endl;
        //  cout<<i<<" dfds"<<endl;
        if(a.size()==1)
        {
            for(auto k:a)
            j=k;
            if(j==1)
            {cout<<i; return 0;}
        }
        else if(a.size()==2)
        {
         // cout<<i<<" dfds"<<endl;
         vl b;
            for(auto k:a)
           b.pb(k);
           sort(all(b));
            if(b[1]-b[0]==1)
             {cout<<i; return 0;}
        }
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#include<stdio.h>
#include<math.h>
#define pb push_back
#define mp make_pair
#define endl '\n'
#define lli long long int
#define li long int
#define FOR(i,n) for (li i = 0; i < n; i++)
#define loop(i,a,b) for (li i = a; i = 0; i--)
#define loopD(i,a,b) for (li i = a; i >= b; i--)
#define vi vector<int>
#define vl vector

#define vll vector<lli>
#define CLEAR(a)  memset(a,0,sizeof(a))
#define CLEARN(a) memset(a,-1,sizeof(a))
#define F first
#define S second
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define mii map<int,int>
#define mll map

#define pii pair<int,int>
#define pll pair

#define all(v) v.begin(),v.end()
#define pq priority_queue

#define spq priority_queue
#define pql priority_queue<pair
>
#define pqs priority_queue<pair
,vector< pair
 >,greater<pair
>>
#define lb(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()
long int mod=1e9+7;
#define PI 3.1415926535897
li i,j,n,m,k,l,t,fre[100050][10];
vl c;
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    c.pb(0);
    cin>>n;
    loop(i,1,n+1)
    {
        cin>>j;
        c.pb(j);
        fre[i][j]=1;
        loop(k,1,11)
            fre[i][k]+=fre[i-1][k];
    }
    //   loop(i,1,n+1){
    //     loop(k,1,11)
    //     cout<<fre[i][k]<<" ";
    //     cout<<endl;}
    //     cout<<endl;
    for(i=n; i>0 ; i--)
    {
        set
a;
        for(j=1 ; j<=10 ; j++){
      //      cout<<j<<" ";
        if(fre[i][j])
        a.insert(fre[i][j]);}
        //  for(auto k:a)
        //  cout<<"ans "<<k<<endl;
        //  cout<<i<<" dfds"<<endl;
        if(a.size()==1)
        {
            for(auto k:a)
            j=k;
            if(j==1)
            {cout<<i; return 0;}
        }
        else if(a.size()==2)
        {
         // cout<<i<<" dfds"<<endl;
         vl b;
            for(auto k:a)
           b.pb(k);
           sort(all(b));
            if(b[1]-b[0]==1 || (b[0]==1 && fre[n][c[i]]==1))
             {cout<<i; return 0;}
        }
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#include<stdio.h>
#include<math.h>
#define pb push_back
#define mp make_pair
#define endl '\n'
#define lli long long int
#define li long int
#define FOR(i,n) for (li i = 0; i < n; i++)
#define loop(i,a,b) for (li i = a; i = 0; i--)
#define loopD(i,a,b) for (li i = a; i >= b; i--)
#define vi vector<int>
#define vl vector

#define vll vector<lli>
#define CLEAR(a)  memset(a,0,sizeof(a))
#define CLEARN(a) memset(a,-1,sizeof(a))
#define F first
#define S second
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define mii map<int,int>
#define mll map

#define pii pair<int,int>
#define pll pair

#define all(v) v.begin(),v.end()
#define pq priority_queue

#define spq priority_queue
#define pql priority_queue<pair
>
#define pqs priority_queue<pair
,vector< pair
 >,greater<pair
>>
#define lb(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()
long int mod=1e9+7;
#define PI 3.1415926535897
li i,j,n,m,k,l,t,fre[100050][10];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    loop(i,1,n+1)
    {
        cin>>j;
        fre[i][j]=1;
        loop(k,1,11)
            fre[i][k]+=fre[i-1][k];
    }
    for(i=n; i>0 ; i--)
    {
        set
ans;
         for(j=1 ; j<=10 ; j++)
         if(fre[i][j])
         ans.insert(fre[i][j]);
         vl rag;
         for(auto k:ans)
         rag.pb(k);
         if(ans.size()==1 && rag[0]==1)
         {cout<<i; return 0;}
         else if(ans.size()==2)
         {
             li flag=0,bha=0;
              for(j=1 ; j<=10 ; j++)
              if(fre[i][j]){
              if(fre[i][j]==rag[0])
              flag++;
                  else if(fre[i][j]==rag[1])
                  bha++;
              }
              if(flag==1)
              {
                  if(rag[0]==1)
                    {cout<<i; return 0;}
                  if(rag[0]-1==rag[1])
                   {cout<<i; return 0;}
              }
               if(bha==1)
              {
                   if(rag[1]==1)
                    {cout<<i; return 0;}
                  if(rag[1]-1==rag[0])
                   {cout<<i; return 0;}
              }

         }
    }
    return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#include<stdio.h>
#include<math.h>
#define pb push_back
#define mp make_pair
#define endl '\n'
#define lli long long int
#define li long int
#define FOR(i,n) for (li i = 0; i < n; i++)
#define loop(i,a,b) for (li i = a; i = 0; i--)
#define loopD(i,a,b) for (li i = a; i >= b; i--)
#define vi vector<int>
#define vl vector

#define vll vector<lli>
#define CLEAR(a)  memset(a,0,sizeof(a))
#define CLEARN(a) memset(a,-1,sizeof(a))
#define F first
#define S second
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define mii map<int,int>
#define mll map

#define pii pair<int,int>
#define pll pair

#define all(v) v.begin(),v.end()
#define pq priority_queue

#define spq priority_queue
#define pql priority_queue<pair
>
#define pqs priority_queue<pair
,vector< pair
 >,greater<pair
>>
#define lb(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()
long int mod=1e9+7;
#define PI 3.1415926535897
li i,j,n,m,k,l,t,fre[100050][10];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    loop(i,1,n+1)
    {
        cin>>j;
        fre[i][j]=1;
        loop(k,1,11)
            fre[i][k]+=fre[i-1][k];
    }
    for(i=n; i>0 ; i--)
    {
        for(j=1 ; j<=10 ; j++)
        {
            if(fre[i][j])
            {
                mll a;
                int flag=0;
                  fre[i][j]--;
                   for(k=1 ; k<=10 ; k++)
                   if(fre[i][k])
                   a[fre[i][k]]++;
                   if(a.size()==1 || a.size()==0)
                   {cout<<i; return 0;}
                   fre[i][j]++;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int u[100005][15];

int main()
{
    int n;
    scanf("%d",&n);
    if(n==1)
    {
        int x;
        scanf("%d",&x);
        cout<<0<<endl;
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        for(int j=1;j<=10;j++)
        {
            u[i][j]=u[i-1][j];
        }
        u[i][x]++;
    }
    int ans=1;
    for(int i=1;i<n;i++)
    {
        int minn=11;
        int maxn=0;
        for(int j=1;j<=10;j++)
        {
            if(u[i][j]!=0)
                minn=min(minn,u[i][j]);
        }
        for(int j=1;j<=10;j++)
        {
            if(u[i][j]!=0)
                maxn=max(maxn,u[i][j]);
        }
        int cnt1=0,cnt2=0;
        for(int j=1;j<=10;j++)
        {
            if(u[i][j]!=0&&u[i][j]!=minn)
                cnt1++;
            if(u[i][j]!=0&&u[i][j]!=maxn)
                cnt2++;
        }
        int cnt=min(cnt1,cnt2);
        if(cnt==1||(minn=1&&cnt==0))
        {
            ans=max(ans,i-cnt+1);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int amp[200005];
bool isrp[20];
int as[20],it,mp[20];
int main() {
    int n;
    cin >> n;
    int tmp,res = 1;
    for(int i = 1; i<=n;i++) {
        cin >> amp[i];
    }
    for(int j = 1; j<=n;j++) {
        mp[amp[j]]++;
        isrp[amp[j]] =true;
        memset(as,0,sizeof(as));
        it=1;
        for(int i =1; i<=n;i++) {
            if(isrp[i]) {
                as[it++] = mp[i];
            }
        }
        it--;
        if(it == 1) {
            res=j;
            continue;
        }
        sort(as+1,as+it+1);
        int k = as[it];
        bool flag = true;
        for(int i = 1; i<it;i++) {
            if(as[i]!=k-1)
                flag = false;
        }
        if(flag) res =j;
        if(!flag && as[1]==1) {
        flag = true;
        for(int i =it;i>=3;i--) {
                if(as[i]!=as[i-1])
                    flag = false;
        }
        if(flag) res = j;
        }
     }
     cout << res <<endl;
     return 0;
}


#include <bits/stdc++.h>
using namespace std;
map<int,int> mp;
int amp[200005];
bool isrp[20];
int as[20],it;
int main() {
    int n;
    cin >> n;
    int tmp,res = 1;
    for(int i = 1; i<=n;i++) {
        cin >> amp[i];
    }
    for(int j = 1; j<=n;j++) {
        mp[amp[j]]++;
        isrp[amp[j]] =true;
        memset(as,0,sizeof(as));
        it=1;
        for(int i =1; i<=n;i++) {
            if(isrp[i]) {
                as[it++] = mp[i];
            }
        }
        it--;
        if(it == 1) {
            res=j;
            continue;
        }
        sort(as+1,as+it+1);
        int k = as[it];
        bool flag = true;
        for(int i = 1; i<it;i++) {
            if(as[i]!=k-1)
                flag = false;
        }
        if(flag) res =j;
        if(!flag && as[1]==1) {
        flag = true;
        for(int i =it;i>=3;i--) {
                if(as[i]!=as[i-1])
                    flag = false;
        }
        if(flag) res = j;
        }
     }
     cout << res <<endl;
     return 0;
}


#include<stdio.h>
int MAX(int x,int y)
{
	return x>y?x:y;
}
int main()
{
	int n,j,pos,mx,a[10000],num[12]={0},c[10000]={0};
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		c[num[a[i]]]--;
		num[a[i]]++;
		c[num[a[i]]]++;
		if(i==1)
			mx=num[a[i]];
		else
			mx=MAX(mx,num[a[i]]);
		if(i==1)
			pos=i;
		else if(c[i]==1||c[1]==i)
			pos=i;
		else if(c[1]==1&&c[mx]*mx==i-1)
			pos=i;
		else if(c[mx]==1&&(mx-1)*c[mx-1]+mx==i)
			pos=i;
	}
	printf("%d\n",pos);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long

int u[100005][15];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        for(int j=1;j<=10;j++)
        {
            u[i][j]=u[i-1][j];
        }
        u[i][x]++;
    }
    int ans=1;
    for(int i=1;i<n;i++)
    {
        int minn=11;
        for(int j=1;j<=10;j++)
        {
            if(u[i][j]!=0)
                minn=min(minn,u[i][j]);
        }
        int cnt=0;
        for(int j=1;j<=10;j++)
        {
            if(u[i][j]!=0&&u[i][j]!=minn)
                cnt++;
        }
        if(cnt==1||(minn=1&&cnt==0))
        {
            ans=max(ans,i-cnt+1);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<memory.h>

using namespace std;

int n,k,m;
bool check;
int a[100010],b[100010],cnt[100010];

void solve()
{
    for(int i=n;i>=1;i--){

        if(b[a[i]]>1&&cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(b[a[i]]!=0&&cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;

        //cout<<k<<" "<<cnt[1]<<endl;
        if((k==1&&cnt[b[a[i]]]==1)||(k==1&&cnt[1]!=0)||(k==2&&cnt[1]==1)||(k==2&&check)){
            cout<<i<<endl;
            return;
        }

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]--;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;
    }
}

int main() {
    scanf("%d",&n); k=0;
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]++;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;
    }

    solve();

    return 0;
}


#include <iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<memory.h>

using namespace std;

int n,k;
bool check;
int a[100010],b[100010],cnt[100010];

void solve()
{
    for(int i=n;i>=1;i--){
        //cout<<k<<" "<<cnt[1]<<endl;
        if((k==1&&cnt[1]!=0)||(k==2&&cnt[1]==1)||(k==2&&check)){
            cout<<i<<endl;
            return;
        }
        cnt[b[a[i]]]--;
        if(cnt[b[a[i]]]==0)k--;
        b[a[i]]--;
        if(b[a[i]]!=0)
            cnt[b[a[i]]]++;
        if(cnt[b[a[i]]]==1)k++;

        if(cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;
    }
}

int main() {
    scanf("%d",&n);
    k=0;
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        b[a[i]]++;
        if(b[a[i]]==1)
            cnt[b[a[i]]]++;
        else{
            cnt[b[a[i]]-1]--;
            if(cnt[b[a[i]]-1]==0)
                k--;
            cnt[b[a[i]]]++;
        }
        if(cnt[b[a[i]]]==1)
            k++;
        if(cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;
    }

    solve();

    return 0;
}


#include <iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<memory.h>

using namespace std;

int n,k;
bool check;
int a[100010],b[100010],cnt[100010];

void solve()
{
    for(int i=n;i>=1;i--){
        //cout<<k<<" "<<cnt[1]<<endl;
        if((k==1&&cnt[1]!=0)||(k==2&&cnt[1]==1)||(k==2&&check)){
            cout<<i<<endl;
            return;
        }

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]--;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;

        if(b[a[i]]>1&&cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(b[a[i]]!=0&&cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;
    }
}

int main() {
    scanf("%d",&n);
    k=0;
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]++;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;

        if(b[a[i]]>1&&cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(b[a[i]]!=0&&cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;
    }

    solve();

    return 0;
}


#include <iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<memory.h>

using namespace std;
int n;
int g[100005];
int mymap[100005];
set<int> s;
int main() {
    scanf("%d",&n);
   memset(mymap,0,sizeof(mymap));
    for(int i=0;i<n;i++) {
        scanf("%d",&g[i]);
        mymap[g[i]]++;
    }
    //x=n是否可行
    //最大值一定是mymap[100004]
    //最小值一定是大于等于1的第一个数
    sort(mymap,mymap+100005);
    int minn=mymap[lower_bound(mymap,mymap+100005,1)-mymap];
    int maxx=mymap[100004];
    cout<<minn<<" "<<maxx<<endl;
    if(minn==maxx&&minn==1) cout<<n;
    else if(minn==1&&mymap[lower_bound(mymap,mymap+100005,1)-mymap+1]==maxx) cout<<n;
    else if(maxx-minn==1&&mymap[100003]==minn) cout<<n;
    //x=n不可行的情况
    else{
        //x至少为2
        for(int ans=n-1;ans>=2;ans--){
            mymap[g[ans]]--;
            cout<<mymap[g[ans]]<<endl;
            sort(mymap,mymap+100005);
            minn=mymap[lower_bound(mymap,mymap+100005,1)-mymap];
            maxx=mymap[100004];
            cout<<minn<<" "<<maxx<<endl;
            if(minn==maxx&&minn==1) {cout<<ans;break;}
            else if(minn==1&&mymap[lower_bound(mymap,mymap+100005,1)-mymap+1]==maxx) {cout<<ans;break;}
            else if(maxx-minn==1&&mymap[100003]==minn) {cout<<ans;break;}
        }
    }
    return 0;
}


#include <iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<memory.h>

using namespace std;

int n,k;
bool check;
int a[100010],b[100010],cnt[100010];

void solve()
{
    for(int i=n;i>=1;i--){

        if(b[a[i]]>1&&cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(b[a[i]]!=0&&cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;

        //cout<<k<<" "<<cnt[1]<<endl;
        if((k==1&&cnt[1]!=0)||(k==2&&cnt[1]==1)||(k==2&&check)){
            cout<<i<<endl;
            return;
        }

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]--;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;
    }
}

int main() {
    scanf("%d",&n); k=0;
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]++;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;
    }

    solve();

    return 0;
}


#include <iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<memory.h>

using namespace std;

int n,k,m;
bool check;
int a[100010],b[100010],cnt[100010];

void solve()
{
    for(int i=n;i>=1;i--){

        if(b[a[i]]>1&&cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(b[a[i]]!=0&&cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;

        //cout<<k<<" "<<cnt[1]<<endl;
        if((k==1&&cnt[b[a[i]]]==1)||(k==2&&cnt[1]==1)||(k==2&&check)){
            cout<<i<<endl;
            return;
        }

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]--;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;
    }
}

int main() {
    scanf("%d",&n); k=0;
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]++;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;
    }

    solve();

    return 0;
}


#include <iostream>
#include<map>
#include<cstring>
#include<cmath>
#include<set>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<memory.h>

using namespace std;

int n,k;
bool check;
int a[100010],b[100010],cnt[100010];

void solve()
{
    for(int i=n;i>=1;i--){

        if(b[a[i]]>1&&cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(b[a[i]]!=0&&cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;

        //cout<<k<<" "<<cnt[1]<<endl;
        if((k==1&&cnt[1]!=0)||(k==2&&cnt[1]==1)||(k==2&&check)){
            cout<<i<<endl;
            return;
        }

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]--;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;
    }
}

int main() {
    scanf("%d",&n); k=0;
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);

        cnt[b[a[i]]]--;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==0)
            k--;

        b[a[i]]++;

        cnt[b[a[i]]]++;
        if(b[a[i]]!=0&&cnt[b[a[i]]]==1)
            k++;

        if(b[a[i]]>1&&cnt[b[a[i]]]==1&&cnt[b[a[i]]-1]!=0)
            check=true;
        else if(b[a[i]]!=0&&cnt[b[a[i]]+1]==1&&cnt[b[a[i]]]!=0)
            check=true;
        else
            check=false;
    }

    solve();

    return 0;
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int maior = 0, menor = 0, tem_um = 0;

		for(int j = 1; j <= 10; j++) {

			if(cont[j] == 1) {
				tem_um++;
				continue;
			}

			if(abs(cont[v[0]] - cont[j]) >= 2) {
				flag = false;
				break;
			}

			if( abs(cont[v[0]] - cont[j]) == 1 ) {
				if(cont[v[0]] > cont[j]) menor++;
				else maior++;
			}

		}

		if(!flag) continue;

		if(menor == 0 or menor == qtd-1 or maior == 1 or tem_um == qtd or tem_um == 1)
			ans = i+1;

	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		int dif = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		int kappa = 0, tem_um = 0;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j]) continue;

			if(cont[j] != cont[v[0]])
				dif++;

			if(cont[j] == 1)
				tem_um++;

			if(abs(cont[j] - cont[v[0]]) == 1)
				kappa = 1;

		}

		if( (dif <= 1 && (tem_um == 1 or tem_um == qtd)) or (dif == 1 && kappa == 1))
			ans = max(i+1LL, ans);

	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int maior = 0, menor = 0, tem_um = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j])
				continue;

			if(cont[j] == 1) {
				tem_um++;
				continue;
			}

			if(abs(cont[v[0]] - cont[j]) >= 2) {
				flag = false;
				break;
			}

			if( abs(cont[v[0]] - cont[j]) == 1 ) {
				if(cont[v[0]] > cont[j]) menor++;
				else maior++;
			}

		}

		if(!flag or (menor && menor < qtd-1) ) continue;

		if(menor == qtd-1 or ((tem_um == qtd-1 or tem_um == 0) && maior == 1) or tem_um >= qtd-1 or (maior + menor == 0 && tem_um == 1) )
			ans = i+1;
	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int maior = 0, menor = 0, tem_um = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j])
				continue;

			if(cont[j] == 1) {
				tem_um++;
				continue;
			}

			if(abs(cont[v[0]] - cont[j]) >= 2) {
				flag = false;
				break;
			}

			if( abs(cont[v[0]] - cont[j]) == 1 ) {
				if(cont[v[0]] > cont[j]) menor++;
				else maior++;
			}

		}

		if(!flag or (menor && menor < qtd-1) ) continue;

		if(menor == qtd-1 or (!(tem_um-(qtd-1)) && maior == 1) or tem_um == qtd or (maior + menor == 0 && tem_um == 1) )
			ans = i+1;
	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int maior = 0, menor = 0, tem_um = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j])
				continue;

			if(cont[j] == 1) {
				tem_um++;
				continue;
			}

			if(abs(cont[v[0]] - cont[j]) >= 2) {
				flag = false;
				break;
			}

			if( abs(cont[v[0]] - cont[j]) == 1 ) {
				if(cont[v[0]] > cont[j]) menor++;
				else maior++;
			}

		}

		if(!flag or (menor >= 1 && menor < qtd-1) ) continue;

		//debug4(menor, maior, tem_um, qtd);
		if(menor == qtd-1 or maior == 1 or tem_um == qtd or tem_um == 1)
			ans = i+1;
	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int dif = 0, tem_um = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j]) continue;

			if(cont[j] == 1) tem_um++;

			else if(abs(cont[j] - cont[v[0]]) > 1) {
				flag = false;
				break;
			}

			if(abs(cont[j] - cont[v[0]]) == 1)
				dif++;

		}

		if(!flag) continue;

		if(dif == 1 or tem_um == 1 or tem_um == qtd)
			ans = max(ans, i+1);
	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int dif = 0, tem_um = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j]) continue;

			if(cont[j] == 1) tem_um++;

			else if(abs(cont[j] - cont[v[0]]) > 1) {
				flag = false;
				break;
			}

			if( abs(cont[j] - cont[v[0]]) )
				dif++;

		}

		if(!flag) continue;

		if(dif == 1 or (!dif && tem_um == qtd) )
			ans = max(ans, i+1);
	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int dif = 0, tem_um = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j]) continue;

			if(cont[j] == 1) tem_um++;

			else if(abs(cont[j] - cont[v[0]]) > 1) {
				flag = false;
				break;
			}

			if(abs(cont[j] - cont[v[0]]) == 1)
				dif++;

		}

		if(!flag) continue;

		if(dif == 1 or (dif <= 1 && tem_um == 1 or tem_um == qtd) )
			ans = max(ans, i+1);
	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int maior = 0, menor = 0, tem_um = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j])
				continue;

			if(cont[j] == 1) {
				tem_um++;
				continue;
			}

			if(abs(cont[v[0]] - cont[j]) >= 2) {
				flag = false;
				break;
			}

			if( abs(cont[v[0]] - cont[j]) == 1 ) {
				if(cont[v[0]] > cont[j]) menor++;
				else maior++;
			}

		}

		if(!flag or (menor && menor < qtd-1) ) continue;

		if(menor == qtd-1 or (!tem_um && maior == 1) or tem_um == qtd or (maior + menor == 0 && tem_um == 1) )
			ans = i+1;
	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


/*
ID: victorn2
LANG: C++
TASK: ratios
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)
#define debug(x) cout << "DEBUG " << x << endl
#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl
#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl
#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl
#define int long long
using namespace std;
typedef pair<ll, ll> pii;
typedef vector<vector<ll>> mat;
const ll mod = 1e9+7;
const int MAXN = 1*1e5+5;
#define fin cin
#define fout cout

void solve() {
	int n;
	cin >> n;
	vector<int> v(n);
	for(int &i : v)
		cin >> i;

	vector<int> cont(12);

	int qtd = 0, ans = 0;

	for(int i = 0; i < n; i++) {

		bool flag = true;
		int maior = 0, menor = 0, tem_um = 0;

		if(!cont[v[i]]) qtd++;
		cont[v[i]]++;

		for(int j = 1; j <= 10; j++) {

			if(!cont[j])
				continue;

			if(cont[j] == 1) {
				tem_um++;
				continue;
			}

			if(abs(cont[v[0]] - cont[j]) >= 2) {
				flag = false;
				break;
			}

			if( abs(cont[v[0]] - cont[j]) == 1 ) {
				if(cont[v[0]] > cont[j]) menor++;
				else maior++;
			}

		}

		if(!flag or (menor && menor < qtd-1) ) continue;

		if(menor == qtd-1 or ((tem_um == qtd-1 or tem_um == 0) && maior == 1) or tem_um == qtd or (maior + menor == 0 && tem_um == 1) )
			ans = i+1;
	}

	cout << ans << endl;
}

int32_t main() {
	int ct = 1;
	// cin >> ct;
	while(ct--)
		solve();
}


#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define M 1000000007
#define endl '\n'
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max((lli)a,max((lli)b,(lli)c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max((lli)a,max((lli)b,max((lli)c,(lli)d)))
#define debug cout<<"HI"<<endl
#define lli long long int
using namespace std;
//#define M 998244353
//cin.ignore()     for getline function
//srand((unsigned) time(0));    To generate different random no everytime
//int randomNumber = rand()%10;
/*bool func(pair < lli,lli > &a , pair < lli,lli > &b)
{
	if(a.first.first==b.first.first)
	return a.second<b.second
	else
	return a.first>b.first;
}*/

lli nCrModpDP(lli n, lli r, lli p)
{
    lli C[r+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;
    for (lli i = 1; i <= n; i++)
    {

        for (lli j = min(i, r); j > 0; j--)

            C[j] = (C[j] + C[j-1])%p;
    }
    return C[r];
}

lli ncr(lli n, lli r, lli p)
{
   if (r==0)
      return 1;
   lli ni = n%p, ri = r%p;

   return (ncr(n/p, r/p, p) *
           nCrModpDP(ni, ri, p)) % p;
}
lli power(lli x,lli n)
{
	lli temp;
	if(n==0)
	return 1;
	else if(n==1)
	return x%M;
	else
	{
		temp=power(x,n/2)%M;
		if(n%2==0)
		return (temp%M*temp%M)%M;
		else
		return (x%M*((temp%M*temp%M)%M)%M)%M;
	}
}
lli isprime(lli n)
{
	lli i;
	for(i=2;i<=(lli)sqrt(n);i++)
	{
		if(n%i==0)
		return 0;
	}
	return 1;
}

struct cmp
{
    bool operator()(pair < lli,lli > a , pair < lli,lli > b)
    {
        if(a.first==b.first)
        return a.second<b.second;
        else
        return a.first>b.first;
    }
};

int main()
{
	//ios_base::sync_with_stdio(false);
    	//cin.tie(NULL);
    	lli n,i,j,g=0,pos,k,temp;
    	cin>>n;
    	lli arr[n];
    	lli a[n][11];
    	for(i=0;i<n;i++)
    	cin>>arr[i];
    	if(n==1)
    	cout<<1<<endl;
    	else
    	{
	    	for(i=0;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		a[i][j]=0;
	    	}
	    	a[0][arr[0]]=1;
	    	for(i=1;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		a[i][j]=a[i-1][j];
	    		a[i][arr[i]]++;
	    	}
	    	/*for(i=0;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		cout<<a[i][j]<<" ";
	    		cout<<endl;
	    	}*/
	    	for(i=n-1;i>=0;i--)
	    	{
	    		for(j=0;j<=10;j++)
	    		{
	    			temp=a[i][j];
	    			if(a[i][j]!=0)
	    			{
	    				a[i][j]--;
	    				set < lli > s;
	    				for(k=0;k<=10;k++)
	    				{
	    					if(a[i][k]!=0)
	    					s.insert(a[i][k]);
	    				}
	    				if(s.size()==1)
	    				{
	    					pos=i+1;
	    					g++;
	    					break;
	    				}
	    			}
	    			if(g!=0)
				break;
	    			a[i][j]=temp;
	    		}
	    		if(g!=0)
	    		break;
	    	}
	    	cout<<pos<<endl;
	}
    	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define M 1000000007
#define endl '\n'
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max((lli)a,max((lli)b,(lli)c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max((lli)a,max((lli)b,max((lli)c,(lli)d)))
#define debug cout<<"HI"<<endl
#define lli long long int
using namespace std;
//#define M 998244353
//cin.ignore()     for getline function
//srand((unsigned) time(0));    To generate different random no everytime
//int randomNumber = rand()%10;
/*bool func(pair < lli,lli > &a , pair < lli,lli > &b)
{
	if(a.first.first==b.first.first)
	return a.second<b.second
	else
	return a.first>b.first;
}*/

lli nCrModpDP(lli n, lli r, lli p)
{
    lli C[r+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;
    for (lli i = 1; i <= n; i++)
    {

        for (lli j = min(i, r); j > 0; j--)

            C[j] = (C[j] + C[j-1])%p;
    }
    return C[r];
}

lli ncr(lli n, lli r, lli p)
{
   if (r==0)
      return 1;
   lli ni = n%p, ri = r%p;

   return (ncr(n/p, r/p, p) *
           nCrModpDP(ni, ri, p)) % p;
}
lli power(lli x,lli n)
{
	lli temp;
	if(n==0)
	return 1;
	else if(n==1)
	return x%M;
	else
	{
		temp=power(x,n/2)%M;
		if(n%2==0)
		return (temp%M*temp%M)%M;
		else
		return (x%M*((temp%M*temp%M)%M)%M)%M;
	}
}
lli isprime(lli n)
{
	lli i;
	for(i=2;i<=(lli)sqrt(n);i++)
	{
		if(n%i==0)
		return 0;
	}
	return 1;
}

struct cmp
{
    bool operator()(pair < lli,lli > a , pair < lli,lli > b)
    {
        if(a.first==b.first)
        return a.second<b.second;
        else
        return a.first>b.first;
    }
};

int main()
{
	ios_base::sync_with_stdio(false);
    	cin.tie(NULL);
    	lli n,i,j,g=0,pos,k,temp;
    	cin>>n;
    	lli arr[n];
    	lli a[n][11];
    	for(i=0;i<n;i++)
    	cin>>arr[i];
    	if(n==1)
    	cout<<1<<endl;
    	else
    	{
	    	for(i=0;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		a[i][j]=0;
	    	}
	    	a[0][arr[0]]=1;
	    	for(i=1;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		a[i][j]=a[i-1][j];
	    		a[i][arr[i]]++;
	    	}
	    	/*for(i=0;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		cout<<a[i][j]<<" ";
	    		cout<<endl;
	    	}*/
	    	for(i=n-1;i>=0;i--)
	    	{
	    		for(j=0;j<=10;j++)
	    		{
	    			temp=a[i][j];
	    			if(a[i][j]!=0)
	    			{
	    				a[i][j]--;
	    				set < lli > s;
	    				for(k=0;k<=10;k++)
	    				{
	    					if(a[i][k]!=0)
	    					s.insert(a[i][k]);
	    				}
	    				if(s.size()==1)
	    				{
	    					pos=i+1;
	    					g++;
	    					break;
	    				}
	    			}
	    			if(g!=0)
				break;
	    			a[i][j]=temp;
	    		}
	    		if(g!=0)
	    		break;
	    	}
	    	cout<<pos<<endl;
	}
    	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define M 1000000007
#define endl '\n'
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max((lli)a,max((lli)b,(lli)c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max((lli)a,max((lli)b,max((lli)c,(lli)d)))
#define debug cout<<"HI"<<endl
#define lli int
using namespace std;
//#define M 998244353
//cin.ignore()     for getline function
//srand((unsigned) time(0));    To generate different random no everytime
//int randomNumber = rand()%10;
/*bool func(pair < lli,lli > &a , pair < lli,lli > &b)
{
	if(a.first.first==b.first.first)
	return a.second<b.second
	else
	return a.first>b.first;
}*/

lli nCrModpDP(lli n, lli r, lli p)
{
    lli C[r+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;
    for (lli i = 1; i <= n; i++)
    {

        for (lli j = min(i, r); j > 0; j--)

            C[j] = (C[j] + C[j-1])%p;
    }
    return C[r];
}

lli ncr(lli n, lli r, lli p)
{
   if (r==0)
      return 1;
   lli ni = n%p, ri = r%p;

   return (ncr(n/p, r/p, p) *
           nCrModpDP(ni, ri, p)) % p;
}
lli power(lli x,lli n)
{
	lli temp;
	if(n==0)
	return 1;
	else if(n==1)
	return x%M;
	else
	{
		temp=power(x,n/2)%M;
		if(n%2==0)
		return (temp%M*temp%M)%M;
		else
		return (x%M*((temp%M*temp%M)%M)%M)%M;
	}
}
lli isprime(lli n)
{
	lli i;
	for(i=2;i<=(lli)sqrt(n);i++)
	{
		if(n%i==0)
		return 0;
	}
	return 1;
}

struct cmp
{
    bool operator()(pair < lli,lli > a , pair < lli,lli > b)
    {
        if(a.first==b.first)
        return a.second<b.second;
        else
        return a.first>b.first;
    }
};

int main()
{
	ios_base::sync_with_stdio(false);
    	cin.tie(NULL);
    	lli n,i,j,g=0,pos,k,temp;
    	cin>>n;
    	lli arr[n];
    	lli a[n][11];
    	for(i=0;i<n;i++)
    	cin>>arr[i];
    	if(n==1)
    	cout<<1<<endl;
    	else
    	{
	    	for(i=0;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		a[i][j]=0;
	    	}
	    	a[0][arr[0]]=1;
	    	for(i=1;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		a[i][j]=a[i-1][j];
	    		a[i][arr[i]]++;
	    	}
	    	/*for(i=0;i<n;i++)
	    	{
	    		for(j=0;j<11;j++)
	    		cout<<a[i][j]<<" ";
	    		cout<<endl;
	    	}*/
	    	for(i=n-1;i>=0;i--)
	    	{
	    		for(j=0;j<=10;j++)
	    		{
	    			temp=a[i][j];
	    			if(a[i][j]!=0)
	    			{
	    				a[i][j]--;
	    				set < lli > s;
	    				for(k=0;k<=10;k++)
	    				{
	    					if(a[i][k]!=0)
	    					s.insert(a[i][k]);
	    				}
	    				if(s.size()==1)
	    				{
	    					pos=i+1;
	    					g++;
	    					break;
	    				}
	    			}
	    			if(g!=0)
				break;
	    			a[i][j]=temp;
	    		}
	    		if(g!=0)
	    		break;
	    	}
	    	cout<<pos<<endl;
	}
    	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define M 1000000007
#define endl '\n'
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max((lli)a,max((lli)b,(lli)c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max((lli)a,max((lli)b,max((lli)c,(lli)d)))
#define debug cout<<"HI"<<endl
#define lli long long int
using namespace std;
//#define M 998244353
//cin.ignore()     for getline function
//srand((unsigned) time(0));    To generate different random no everytime
//int randomNumber = rand()%10;
/*bool func(pair < lli,lli > &a , pair < lli,lli > &b)
{
	if(a.first.first==b.first.first)
	return a.second<b.second
	else
	return a.first>b.first;
}*/

lli nCrModpDP(lli n, lli r, lli p)
{
    lli C[r+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;
    for (lli i = 1; i <= n; i++)
    {

        for (lli j = min(i, r); j > 0; j--)

            C[j] = (C[j] + C[j-1])%p;
    }
    return C[r];
}

lli ncr(lli n, lli r, lli p)
{
   if (r==0)
      return 1;
   lli ni = n%p, ri = r%p;

   return (ncr(n/p, r/p, p) *
           nCrModpDP(ni, ri, p)) % p;
}
lli power(lli x,lli n)
{
	lli temp;
	if(n==0)
	return 1;
	else if(n==1)
	return x%M;
	else
	{
		temp=power(x,n/2)%M;
		if(n%2==0)
		return (temp%M*temp%M)%M;
		else
		return (x%M*((temp%M*temp%M)%M)%M)%M;
	}
}
lli isprime(lli n)
{
	lli i;
	for(i=2;i<=(lli)sqrt(n);i++)
	{
		if(n%i==0)
		return 0;
	}
	return 1;
}

struct cmp
{
    bool operator()(pair < lli,lli > a , pair < lli,lli > b)
    {
        if(a.first==b.first)
        return a.second<b.second;
        else
        return a.first>b.first;
    }
};

int main()
{
	//ios_base::sync_with_stdio(false);
    	//cin.tie(NULL);
    	lli n,i,j,g=0,pos;
    	cin>>n;
    	lli arr[n];
    	lli a[n][11];
    	for(i=0;i<n;i++)
    	cin>>arr[i];
    	for(i=0;i<n;i++)
    	{
    		for(j=0;j<11;j++)
    		a[i][j]=0;
    	}
    	a[0][arr[0]]=1;
    	for(i=1;i<n;i++)
    	{
    		for(j=0;j<11;j++)
    		a[i][j]=a[i-1][j];
    		a[i][arr[i]]++;
    	}
    	/*for(i=0;i<n;i++)
    	{
    		for(j=0;j<11;j++)
    		cout<<a[i][j]<<" ";
    		cout<<endl;
    	}*/
    	for(i=n-1;i>=0;i--)
    	{
    		map < lli ,lli > mp;
    		map < lli ,lli > :: iterator itr;
    		for(j=0;j<11;j++)
    		{
    			if(a[i][j]!=0)
    			mp[a[i][j]]++;
    		}
    		if(mp.size()==2)
    		{
    			vector < lli > v;
    			for(itr=mp.begin();itr!=mp.end();itr++)
    			v.pb(itr->first);
    			if(v[0]-v[1]==1||v[0]-v[1]==-1)
    			{
    				g++;
    				pos=i+1;
    				break;
    			}
    		}
    	}
    	if(g==0)
    	cout<<n<<endl;
    	else
    	cout<<pos<<endl;
    	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define M 1000000007
#define endl '\n'
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max((lli)a,max((lli)b,(lli)c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max((lli)a,max((lli)b,max((lli)c,(lli)d)))
#define debug cout<<"HI"<<endl
#define lli long long int
using namespace std;
//#define M 998244353
//cin.ignore()     for getline function
//srand((unsigned) time(0));    To generate different random no everytime
//int randomNumber = rand()%10;
/*bool func(pair < lli,lli > &a , pair < lli,lli > &b)
{
	if(a.first.first==b.first.first)
	return a.second<b.second
	else
	return a.first>b.first;
}*/

lli nCrModpDP(lli n, lli r, lli p)
{
    lli C[r+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;
    for (lli i = 1; i <= n; i++)
    {

        for (lli j = min(i, r); j > 0; j--)

            C[j] = (C[j] + C[j-1])%p;
    }
    return C[r];
}

lli ncr(lli n, lli r, lli p)
{
   if (r==0)
      return 1;
   lli ni = n%p, ri = r%p;

   return (ncr(n/p, r/p, p) *
           nCrModpDP(ni, ri, p)) % p;
}
lli power(lli x,lli n)
{
	lli temp;
	if(n==0)
	return 1;
	else if(n==1)
	return x%M;
	else
	{
		temp=power(x,n/2)%M;
		if(n%2==0)
		return (temp%M*temp%M)%M;
		else
		return (x%M*((temp%M*temp%M)%M)%M)%M;
	}
}
lli isprime(lli n)
{
	lli i;
	for(i=2;i<=(lli)sqrt(n);i++)
	{
		if(n%i==0)
		return 0;
	}
	return 1;
}

struct cmp
{
    bool operator()(pair < lli,lli > a , pair < lli,lli > b)
    {
        if(a.first==b.first)
        return a.second<b.second;
        else
        return a.first>b.first;
    }
};

int main()
{
	//ios_base::sync_with_stdio(false);
    	//cin.tie(NULL);
    	lli n,i,j,g=0,pos;
    	cin>>n;
    	lli arr[n];
    	lli a[n][11];
    	for(i=0;i<n;i++)
    	cin>>arr[i];
    	for(i=0;i<n;i++)
    	{
    		for(j=0;j<11;j++)
    		a[i][j]=0;
    	}
    	a[0][arr[0]]=1;
    	for(i=1;i<n;i++)
    	{
    		for(j=0;j<11;j++)
    		a[i][j]=a[i-1][j];
    		a[i][arr[i]]++;
    	}
    	/*for(i=0;i<n;i++)
    	{
    		for(j=0;j<11;j++)
    		cout<<a[i][j]<<" ";
    		cout<<endl;
    	}*/
    	for(i=n-1;i>=0;i--)
    	{
    		map < lli ,lli > mp;
    		map < lli ,lli > :: iterator itr;
    		for(j=0;j<11;j++)
    		{
    			if(a[i][j]!=0)
    			mp[a[i][j]]++;
    		}
    		if(mp.size()==2)
    		{
    			vector < lli > v;
    			for(itr=mp.begin();itr!=mp.end();itr++)
    			v.pb(itr->first);
    			if(v[0]-v[1]==1||v[0]-v[1]==-1||(v[0]==1&&mp[v[0]]==1)||(v[1]==1&&mp[v[1]]==1))
    			{
    				if(mp[v[0]]==1||mp[v[1]]==1)
    				{
    					g++;
    					pos=i+1;
    					break;
    				}
    			}
    		}
    	}
    	if(g==0)
    	cout<<n<<endl;
    	else
    	cout<<pos<<endl;
    	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265

int lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main() {
    IOS;
	int t;
	t=1;
	//cin>>t;
	while (t--){
	    int n, i, ans;
	    cin>>n;
	    mii m;
	    for (i=1; i<=n; i++){
	        int color, mi=INT_MAX, ma=-1;
	        cin>>color;
	        m[color]++;
	        for (auto it=m.begin(); it!=m. end(); it++){
	            mi=min(mi, it->second);
	            ma=max(ma, it->second);
	        }
	        if (i==m.size()){
	            if (i==n)
	            ans=i;
	            else ans=i+1;
	        }
	        else if (m.size()==1){
	            if (i==n)
	            ans=i;
	            else ans=i+1;
	        }
	        else if (ma-mi==1) ans=i;
	        else if (ma-mi==0){
	            if (i==n){

	                ans=n-1;
	            }
	            else ans=i+1;
	        }
	    }
	    cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265

int lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main() {
    IOS;
	int t;
	t=1;
	//cin>>t;
	while (t--){
	    int n, i, ans;
	    cin>>n;
	    mii m;
	    for (i=1; i<=n; i++){
	        int color, mi=INT_MAX, ma=-1;
	        cin>>color;
	        m[color]++;
	        int count=0;
	        for (auto it=m.begin(); it!=m. end(); it++){
	            mi=min(mi, it->second);
	            ma=max(ma, it->second);
	        }
	        for (auto it=m.begin(); it!=m. end(); it++){
	            if (it->second==ma) count++;
	        }
	        if (ma-mi==1 && count>1) continue;
	        if (ma-mi==1) ans=i;
	        else if (ma-mi==0){
	            if (m.size()==i){
	                if (i==n) ans=i;
	                else ans=i+1;
	            }
	            else{
	                if (i==n) ans=i-1;
	                else ans=i+1;
	            }
	        }
	    }
	    cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265

int lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main() {
    IOS;
	int t;
	t=1;
	//cin>>t;
	while (t--){
	    int n, i, ans;
	    cin>>n;
	    mii m;
	    for (i=1; i<=n; i++){
	        int color, mi=INT_MAX, ma=-1;
	        cin>>color;
	        m[color]++;
	        for (auto it=m.begin(); it!=m. end(); it++){
	            mi=min(mi, it->second);
	            ma=max(ma, it->second);
	        }
	        if (ma-mi==1) ans=i;
	        else if (ma-mi==0){
	            if (m.size()==i){
	                if (i==n) ans=i;
	                else ans=i+1;
	            }
	            else{
	                if (i==n) ans=i-1;
	                else ans=i+1;
	            }
	        }
	    }
	    cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265

int lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main() {
    IOS;
	int t;
	t=1;
	//cin>>t;
	while (t--){
	    int n, i, ans;
	    cin>>n;
	    mii m;
	    for (i=1; i<=n; i++){
	        int color, mi=INT_MAX, ma=-1;
	        cin>>color;
	        m[color]++;
	        for (auto it=m.begin(); it!=m. end(); it++){
	            mi=min(mi, it->second);
	            ma=max(ma, it->second);
	        }
	        if ((ma-mi)==0){
	        ans=i+1;
	        if (i==n) ans--;
	        }
	        else if ((ma-mi)==1) ans=i;

	    }
	    if (ans>n) ans=n;
	    cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265

int lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main() {
    IOS;
	int t;
	t=1;
	//cin>>t;
	while (t--){
	    int n, i, ans;
	    cin>>n;
	    mii m;
	    for (i=1; i<=n; i++){
	        int color, mi=INT_MAX, ma=-1;
	        cin>>color;
	        m[color]++;
	        for (auto it=m.begin(); it!=m. end(); it++){
	            mi=min(mi, it->second);
	            ma=max(ma, it->second);
	        }
	        if ((ma-mi)==0) ans=i+1;
	        else if ((ma-mi)==1) ans=i;

	    }
	    if (ans>n) ans=n;
	    cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265

int lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main() {
    IOS;
	int t;
	t=1;
	//cin>>t;
	while (t--){
	    int n, i, ans;
	    cin>>n;
	    mii m;
	    for (i=1; i<=n; i++){
	        int color, mi=INT_MAX, ma=-1;
	        cin>>color;
	        m[color]++;
	        for (auto it=m.begin(); it!=m. end(); it++){
	            mi=min(mi, it->second);
	            ma=max(ma, it->second);
	        }
	        if (i==m.size()) ans=i;
	        else if (m.size()==1) ans=i;
	        else if (ma-mi==1) ans=i;
	        else if (ma-mi==0){
	            if (i==n){

	                ans=n-1;
	            }
	            else ans=i+1;
	        }
	    }
	    cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define VI vector <int>
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define setbits(n) __builtin_popcount(n)
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265
const long long N=1e5+5;

ll lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main(){
    IOS;
	int t;
	t=1;
// 	cin>>t;
	while (t--){
	    int n;
	    cin>>n;
	    int cnt[N]={}, ccnt[N]={};
	    int ans=1, mx=0;
	    for (int i=0; i<n; i++){
	        int x;
	        cin>>x;
	        ccnt[cnt[x]]--;
	        cnt[x]++;
	        ccnt[cnt[x]]++;
	        bool ok=false;
	        mx=max(mx, cnt[x]);
	        if (mx==1) ok=true;                                        // only 1 colour
	        else if (ccnt[mx]==1 && ccnt[mx-1]*(mx-1)==i-mx+1) ok=true;    //one max and rest  max-1)
	        else if(ccnt[1]==i+1) ok=true;                            //all occurence 1 time
	        else if(ccnt[1]=1 && ccnt[mx]*mx==i) ok=true;             //one 1 and rest max

	        if (ok) ans=i+1;
	    }
	    cout<<ans<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265

int lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main() {
    IOS;
	int t;
	t=1;
	//cin>>t;
	while (t--){
	    int n, i, ans;
	    cin>>n;
	    mii m;
	    for (i=1; i<=n; i++){
	        int color, mi=INT_MAX, ma=-1;
	        cin>>color;
	        m[color]++;
	        for (auto it=m.begin(); it!=m. end(); it++){
	            mi=min(mi, it->second);
	            ma=max(ma, it->second);
	        }
	        if (i==m.size()){
	            if (i==n)
	            ans=i;
	            else i+1;
	        }
	        else if (m.size()==1) ans=i;
	        else if (ma-mi==1) ans=i;
	        else if (ma-mi==0){
	            if (i==n){

	                ans=n-1;
	            }
	            else ans=i+1;
	        }
	    }
	    cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pci pair<char, int>
#define mii map<int, int>
#define mci map<char, int>
#define fill(a,b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265

int lcm(ll a, ll b){
    ll g=__gcd(a, b);
    return a/g*b;
}

int main() {
    IOS;
	int t;
	t=1;
	//cin>>t;
	while (t--){
	    int n, i, ans;
	    cin>>n;
	    mii m;
	    for (i=1; i<=n; i++){
	        int color, mi=INT_MAX, ma=-1;
	        cin>>color;
	        m[color]++;
	        for (auto it=m.begin(); it!=m. end(); it++){
	            mi=min(mi, it->second);
	            ma=max(ma, it->second);
	        }
	        if (i==m.size()){
	            if (i==n)
	            ans=i;
	            else ans=i+1;
	        }
	        else if (m.size()==1) ans=i;
	        else if (ma-mi==1) ans=i;
	        else if (ma-mi==0){
	            if (i==n){

	                ans=n-1;
	            }
	            else ans=i+1;
	        }
	    }
	    cout<<ans;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector <int>> vvi;
typedef vector<vector <ll>> vvll;
bool cmp(int a,int b)
{return a>b;
}
int main()
{
	int t;
	cin>>t;
	int ans,answer;
	vi v;
	int n,arr[11]={};
	for(int i=0;i<t;i++)
	{
		cin>>n;
		int j=1,ch=0;
		arr[n]++;
		v.clear();
		for(int j=0;j<11;j++)
		v.push_back(arr[j]);
		sort(v.begin(),v.end(),cmp);
		//for(auto u:v)
		//cout<<u<<' ';
		//cout<<'\n';
	    if(v[0]==1)
	    answer=i+1;
	    else
	    if(v[j]!=ans)
	    { ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans-1)
			{
				ch=1;
				break;
			}
			j++;
		}
	    if(ch==1)
		continue;
		else
		answer=i+1;
	  }
	  else
	  {

		  ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans)
			{
				ch=1;
				break;
			}
			j++;
		}

	    if(ch==1 && v[j]==1 && v[j+1]==0)
		answer=i+1;
		else
		continue;

	  }

}
cout<<answer;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector <int>> vvi;
typedef vector<vector <ll>> vvll;
bool cmp(int a,int b)
{return a>b;
}
int main()
{
	int t;
	cin>>t;
	int ans,answer;
	vi v;
	int n,arr[11]={};
	for(int i=0;i<t;i++)
	{
		cin>>n;
		int j=1,ch=0;
		arr[n]++;
		v.clear();
		for(int j=0;j<11;j++)
		v.push_back(arr[j]);
		sort(v.begin(),v.end(),cmp);
		//for(auto u:v)
		//cout<<u<<' ';
		//cout<<'\n';
	    if(v[0]==1)
	    answer=i+1;
	    else
	    if(v[j]!=ans)
	    { ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans-1)
			{
				ch=1;
				break;
			}
			j++;
		}
	    if(ch==1)
		continue;
		else
		answer=i+1;
	  }
	  else
	  {

		  ans=v[0];
	    while(v[j])
	    {
			if(v[j]!=ans)
			{
				ch=1;
				break;
			}
			j++;
		}
		if(v[j]==0 && j==1)
		answer=i+1;
		else

	    if(ch==1 && v[j]==1 && v[j+1]==0)
		answer=i+1;
		else
		continue;

	  }

}
cout<<answer;
cout<<'\n';
return 0;
}