/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
#define all(a) a.begin(),a.end()
using namespace std;

const ll maxn = 1e5;

int main(){
  int n;
  cin >> n;
  V<int> arr(n);
  loop(i,0,n)cin >> arr[i];
  unordered_map<int,int> m;
  int ans = 0;
  loop(i,1,n+1){
    m[arr[i-1]]++;
    unordered_map<int,int> m1;
    for(auto &j: m){
      m1[j.S]++;
    }
    if(m1.size() == 2){
      bool p = true;
      PII a,b;
      for(auto j: m1){
        if(p){
          a = mp(j.F,j.S);
          p = false;
        }else{
          b = mp(j.F,j.S);
        }
      }
      if((abs(a.F - b.F) == 1 && (a.S == 1 || b.S == 1)) || (a.F == 1 && a.S == 1) || (b.F == 1 && b.S == 1)){
        ans = max(i,ans);
      }
    }else if(m1.size() == 1){
      if(m1.begin()->F == 1){
        ans = max(ans,i);
      }
    }
  }
  cout << ans;
  return 0;
}


/*
 ____________________________________________________________
|                                                            |
|                   Author: ay2306                           |
|____________________________________________________________|

*/
#include <bits/stdc++.h>
#define MOD 1000000007
#define test int t; cin>>t; while(t--)
#define init(arr,val) memset(arr,val,sizeof(arr))
#define loop(i,a,b) for(int i=a;i<b;i++)
#define loopr(i,a,b) for(int i=a;i>=b;i--)
#define loops(i,a,b,step) for(int i=a;i<b;i+=step)
#define looprs(i,a,b,step) for(int i=a;i>=b;i-=step)
#define ull unsigned long long int
#define ll long long int
#define P pair
#define PLL pair<long long, long long>
#define PII pair<int, int>
#define PUU pair<unsigned long long int, unsigned long long int>
#define L list
#define V vector
#define D deque
#define ST set
#define MS multiset
#define M map
#define UM unordered_map
#define mp make_pair
#define pb push_back
#define pf push_front
#define MM multimap
#define F first
#define S second
#define IT iterator
#define RIT reverse_iterator
#define FAST ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define FILE_READ_IN freopen("input.txt","r",stdin);
#define FILE_READ_OUT freopen("output.txt","w",stdout);
#define all(a) a.begin(),a.end()
using namespace std;

const ll maxn = 1e5;

int main(){
  int n;
  cin >> n;
  V<int> arr(n);
  loop(i,0,n)cin >> arr[i];
  unordered_map<int,int> m;
  int ans = 0;
  loop(i,1,n+1){
      m[arr[i-1]]++;
      loop(k,1,11){
          if(m.find(k) == m.end())continue;
          m[k]--;
          if(m[k] == 0)m.erase(k);
          unordered_map<int,int> m1;
          for(auto &j: m){
              m1[j.S]++;
          }
          if(m1.size() == 1){
              ans = max(ans,i);
          }
          m[k]++;
          if(ans == i)break;
      }
  }
  cout << ans;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int a[maxn];
int pre[maxn][11];
inline int judge(int x)
{
    multiset<int> s1;
    set<int> s2;
    s1.clear(), s2.clear();
    for (int i = 1; i <= 10; i++)
    {
        int t = pre[x][i];
        if (t)
            s1.insert(t), s2.insert(t);
    }
    if (s2.size()>2)
        return false;
    if (s2.size()==1){
        return s2.count(1);
    }
    int t1 = 0, t2 = 0;
    int x1 = *(s2.begin());
    s2.erase(s2.begin());
    int x2 = *(s2.begin());
    s2.erase(s2.begin());
    t1 = s1.count(x1), t2 = s1.count(x2);
    if (t1==1&&x1==1)
        return true;
    if (t2==1&&x1+1==x2)
        return true;
    return false;
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        for (int j = 1; j <= 10; j++)
            pre[i][j] = pre[i - 1][j];
        ++pre[i][a[i]];
    }
    for (int last = n; last >= 1; last--)
    {
        if (judge(last))
        {
            cout << last;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int a[maxn];
int pre[maxn][11];
unordered_multiset<int> s[maxn];
inline int judge(int x){
    if (x==1)
        return 1;
    vector<int> now;
    now.clear();
    for (int i = 1; i <= 10;i++){
        int k = s[x].count(i);
        if (k)
            now.push_back(k);
    }
    sort(now.begin(), now.end());
    int tot = 0;
    for (auto t:now)
        if (t==1)
            ++tot;
    vector<int> cur = now;
    auto last = unique(now.begin(), now.end());
    if (last-now.begin()>2)
        return false;
    if (last-now.begin()==1&&now.front()==1)
        return true;
    else if (last-now.begin()==1)
        return false;
    int t1 = 0, t2 = 0;
    for (int t:cur)
        if (t==now.front())
            ++t1;
        else
            ++t2;
   if (t1==1&&now.front()==1)
       return true;
    if (t2==1&&(*(last-1)==now.front()+1))
        return true;
    return false;
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n;i++){
        cin >> a[i];
        s[i] = s[i - 1];
        s[i].insert(a[i]);
        for (int j = 1; j <= 10;j++)
            pre[i][j] = pre[i - 1][j];
        ++pre[i][a[i]];
    }
    for (int last = n; last >= 1;last--){
        if (judge(last)){
            cout << last;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int a[maxn];
int pre[maxn][11];
inline int judge(int x)
{
    multiset<int> s1;
    set<int> s2;
    s1.clear(), s2.clear();
    for (int i = 1; i <= 10; i++)
    {
        int t = pre[x][i];
        if (t)
            s1.insert(t), s2.insert(t);
    }
    if (s2.size()>2)
        return false;
    if (s2.size()==1){
        return s2.count(1);
    }
    int t1 = 0, t2 = 0;
    int x1 = *(s2.begin());
    s2.erase(s2.begin());
    int x2 = *(s2.begin());
    s2.erase(s2.begin());
    t1 = s1.count(x1), t2 = s1.count(x2);
    if (t1==1&&x1==1)
        return true;
    if (t2==1&&abs(x1-x2)==1)
        return true;
    return false;
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        for (int j = 1; j <= 10; j++)
            pre[i][j] = pre[i - 1][j];
        ++pre[i][a[i]];
    }
    for (int last = n; last >= 1; last--)
    {
        if (judge(last))
        {
            cout << last;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int num[maxn];
unordered_map<int, int> mp;
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    int ans;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        if (num[x]!=0)
            mp[num[x]]--;
        if (!mp[num[x]])
            mp.erase(num[x]);
        ++num[x];
        ++mp[num[x]];
        if (mp.size()>2)
            continue;
        if (mp.size()==2){
            auto cur = mp.begin();
            auto f = cur++;
            auto e = cur;
            if (f->first==1&&f->second==1)
                ans = i;
            if (f->first==e->first-1&&e->second==1)
                ans = i;
        }
        else if (mp.size()==1){
            auto f = mp.begin();
            if (f->first==1||f->second==1)
                ans = i;
            continue;
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int a[maxn];
int pre[maxn][11];
inline int judge(int x)
{
    multiset<int> s1;
    set<int> s2;
    s1.clear(), s2.clear();
    for (int i = 1; i <= 10; i++)
    {
        int t = pre[x][i];
        if (t)
            s1.insert(t), s2.insert(t);
    }
    if (s2.size()>2)
        return false;
    if (s2.size()==1){
        int k = s1.count(*s2.begin());
        return k == 1||*s2.begin()==1;
    }
    int t1 = 0, t2 = 0;
    int x1 = *(s2.begin());//最小出现次数
    s2.erase(s2.begin());
    int x2 = *(s2.begin());//最大出现次数
    s2.erase(s2.begin());
    t1 = s1.count(x1), t2 = s1.count(x2);
    if (t1==1&&x1==1)
        return true;
    if (t2==1&&abs(x1-x2)==1)
        return true;
    return false;
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        for (int j = 1; j <= 10; j++)
            pre[i][j] = pre[i - 1][j];
        ++pre[i][a[i]];
    }
    for (int last = n; last >= 1; last--)
    {
        if (judge(last))
        {
            cout << last;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int a[maxn];
int pre[maxn][11];
inline int judge(int x)
{
    multiset<int> s1;
    set<int> s2;
    s1.clear(), s2.clear();
    for (int i = 1; i <= 10; i++)
    {
        int t = pre[x][i];
        if (t)
            s1.insert(t), s2.insert(t);
    }
    if (s2.size()>2)
        return false;
    if (s2.size()==1){
        int k = s1.count(*s2.begin());
        return k == 1;
    }
    int t1 = 0, t2 = 0;
    int x1 = *(s2.begin());//最小出现次数
    s2.erase(s2.begin());
    int x2 = *(s2.begin());//最大出现次数
    s2.erase(s2.begin());
    t1 = s1.count(x1), t2 = s1.count(x2);
    if (t1==1&&x1==1)
        return true;
    if (t2==1&&abs(x1-x2)==1)
        return true;
    return false;
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        for (int j = 1; j <= 10; j++)
            pre[i][j] = pre[i - 1][j];
        ++pre[i][a[i]];
    }
    for (int last = n; last >= 1; last--)
    {
        if (judge(last))
        {
            cout << last;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int a[maxn];
int pre[maxn][11];
unordered_multiset<int> s[maxn];
inline int judge(int x){
    if (x==1)
        return 1;
    vector<int> now;
    now.clear();
    for (int i = 1; i <= 10;i++){
        int k = s[x].count(i);
        if (k)
            now.push_back(k);
    }
    sort(now.begin(), now.end());
    int maxx = *max_element(now.begin(), now.end());
    int minx = *min_element(now.begin(), now.end());
    int totMaxx = 0, totMinx = 0;
    for (auto t:now)
        if (t==maxx)
            ++totMaxx;
        else if (t==minx)
            ++totMinx;
    return (maxx == minx + 1 && (totMaxx == 1||totMinx==1)) || (maxx == minx&&maxx==1) || (maxx == 1 && totMaxx == 1) || (minx == 1 && totMinx == 1);
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n;i++){
        cin >> a[i];
        s[i] = s[i - 1];
        s[i].insert(a[i]);
        for (int j = 1; j <= 10;j++)
            pre[i][j] = pre[i - 1][j];
        ++pre[i][a[i]];
    }
    for (int last = n; last >= 1;last--){
        if (judge(last)){
            cout << last;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int num[maxn];
unordered_map<int, int> mp;
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    int ans;
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        if (num[x]!=0)
            mp[num[x]]--;
        if (!mp[num[x]])
            mp.erase(num[x]);
        ++num[x];
        ++mp[num[x]];
        if (mp.size()>2)
            continue;
        if (mp.size()==2){
            auto cur = mp.begin();
            auto f = cur++;
            auto e = cur;
            if (f->first==1&&f->second==1)
                ans = i;
            if (f->first==e->first-1&&e->second==1)
                ans = i;
            if (e->first==1&&e->second==1)
                ans = i;
        }
        else if (mp.size()==1){
            auto f = mp.begin();
            if (f->first==1||f->second==1)
                ans = i;
            continue;
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int a[maxn];
int pre[maxn][11];
unordered_multiset<int> s[maxn];
inline int judge(int x)
{
    vector<int> now;
    now.clear();
    for (int i = 1; i <= 10; i++)
    {
        int t = pre[x][i];
        if (t)
            now.push_back(t);
    }
    sort(now.begin(), now.end());
    int tot = 0;
    for (auto t : now)
        if (t == 1)
            ++tot;
    vector<int> cur = now;
    auto last = unique(now.begin(), now.end());
    if (last - now.begin() > 2)
        return false;
    if (last - now.begin() == 1)
        return now.front() == 1;
    int t1 = 0, t2 = 0;
    for (int t : cur)
        if (t == now.front())
            ++t1;
        else
            ++t2;
    if (t1 == 1 && now.front() == 1)
        return true;
    if (t2 == 1 && (*(last - 1) == now.front() + 1))
        return true;
    return false;
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        for (int j = 1; j <= 10; j++)
            pre[i][j] = pre[i - 1][j];
        ++pre[i][a[i]];
    }
    for (int last = n; last >= 1; last--)
    {
        if (judge(last))
        {
            cout << last;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int a[maxn];
int pre[maxn][11];
unordered_multiset<int> s[maxn];
inline int judge(int x){
    if (x==1)
        return 1;
    vector<int> now;
    now.clear();
    for (int i = 1; i <= 10;i++){
        int t = pre[x][i];
        if (t)
            now.push_back(t);
    }
    sort(now.begin(), now.end());
    int tot = 0;
    for (auto t:now)
        if (t==1)
            ++tot;
    vector<int> cur = now;
    auto last = unique(now.begin(), now.end());
    if (last-now.begin()>2)
        return false;
    if (last-now.begin()==1&&now.front()==1)
        return true;
    else if (last-now.begin()==1)
        return false;
    int t1 = 0, t2 = 0;
    for (int t:cur)
        if (t==now.front())
            ++t1;
        else
            ++t2;
   if (t1==1&&now.front()==1)
       return true;
    if (t2==1&&(*(last-1)==now.front()+1))
        return true;
    return false;
}
int main(int argc, char const *argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n;i++){
        cin >> a[i];
        for (int j = 1; j <= 10;j++)
            pre[i][j] = pre[i - 1][j];
        ++pre[i][a[i]];
    }
    for (int last = n; last >= 1;last--){
        if (judge(last)){
            cout << last;
            return 0;
        }
    }
    return 0;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<< #x << " --> " << x << "\n";
#define NL printf("\n");
#define case(x) printf("Case %lld: ",x);
#define readI(x) scanf("%d",&x);
#define readL(x) scanf("%lld",&x);
#define writeI(x) printf("%d",x);
#define writeL(X) printf("%lld",x);
#define all(v) v.begin(),v.end()

#define ll long long
#define ld long double
#define pb push_back
#define pii pair< int,int >
#define pll pair< ll,ll >
#define vii vector< int >
#define vll vector< ll >
#define vss vector< string >
#define vdd vector< double >
#define vpi vector< pii >
#define vpl vector< pll >
#define vvi vector< vii >
#define vvl vector< vll >
#define PQ priority_queue
#define ff first
#define ss second
#define MX 100005
#define mod 1000000007
#define INF 10000000000000000
#define EPS 1e-12

/* Special functions:
		find_by_order(k) --> returns iterator to the kth largest element counting from 0
		order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/

typedef tree<
ll,					// type long long
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

struct compare
{
    bool operator() (ll a, ll b)
    {
        return a>b;
    }
};

const ll p1=131,p2=137;
const ll mod1=1000000009;
const ll mod2=1000000007;

bool marked[MX];
vll primes;

ll bigMod(ll x, ll n)
{
    if(!n)
        return 1LL;
    ll temp=bigMod(x,n/2);
    temp=(temp*temp)%mod;
    if(n%2)
        temp=(temp*x)%mod;
    return temp;
}

void sieve()
{
    marked[0]=marked[1]=true;
    for(ll i=2;i*i<MX;i++){
        if(marked[i]==false){
            for(ll j=i*i;j<MX;j+=i){
                marked[j]=true;
            }
        }
    }

    for(ll i=2;i<MX;i++)
        if(!marked[i]){
            primes.push_back(i);
        }
}

vpl primeFactors(ll N)
{
	vpl factors;
	ll pf_id=0,pf=primes[pf_id];
	while(pf*pf<=N){
        ll cnt=0;
		while(N%pf==0) N/=pf,cnt++;
        if(cnt) factors.push_back({pf,cnt});

		pf=primes[++pf_id];
	}

	if(N!=1) factors.push_back({N,1});
    return factors;
}

struct pair_hash {
    template <class T1, class T2>
    size_t operator () (const pair<T1,T2> &p) const {
        auto h1 = hash<T1>{}(p.first);
        auto h2 = hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};

struct chash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	// Note: when casting y to unsigned x, x will be least unsigned int congruent
	// to y mod 2^64.
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
gp_hash_table<ll, ll, chash>var;

                        /**---------End of Template---------**/

ll cnt[MX];
ll vcnt[MX];

int main()
{

    FIO;

    ll n;

    cin>> n;
    vll v(n);
    set<ll>cc;
    for(ll i=0;i<n;i++) cin>> v[i],cc.insert(v[i]);
    vcnt[0]=cc.size();
    set<ll>st;

    ll ans=0;
    for(ll i=0;i<n;i++){
        st.insert(v[i]);
        vcnt[cnt[v[i]]]--;
        cnt[v[i]]++;
        vcnt[cnt[v[i]]]++;

        if(st.size()==1){
            ans=i+1;
            continue;
        }

        if(vcnt[1]==st.size()){
            ans=i+1;
            continue;
        }

        if(vcnt[cnt[v[i]]]==st.size()-1) {
            ll temp=(st.size()-1)*cnt[v[i]];
            ll x=i+1-temp;
            if(x-1==0 or x-1==cnt[v[i]])
                ans=i+1;
        }
        if(true){
            ll temp=-1;
            for(auto x:st){
                if(x!=v[i]){
                    temp=x;
                    break;
                }
            }

            if(vcnt[cnt[temp]]==st.size()-1){
                ll t=temp;
                temp=(st.size()-1)*cnt[t];
                ll x=i+1-temp;
                if(x-1==0 or x-1==cnt[t])
                    ans=i+1;
            }
        }
    }

    cout<< ans;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<< #x << " --> " << x << "\n";
#define NL printf("\n");
#define case(x) printf("Case %lld: ",x);
#define readI(x) scanf("%d",&x);
#define readL(x) scanf("%lld",&x);
#define writeI(x) printf("%d",x);
#define writeL(X) printf("%lld",x);
#define all(v) v.begin(),v.end()

#define ll long long
#define ld long double
#define pb push_back
#define pii pair< int,int >
#define pll pair< ll,ll >
#define vii vector< int >
#define vll vector< ll >
#define vss vector< string >
#define vdd vector< double >
#define vpi vector< pii >
#define vpl vector< pll >
#define vvi vector< vii >
#define vvl vector< vll >
#define PQ priority_queue
#define ff first
#define ss second
#define MX 2000005
#define mod 1000000007
#define INF 10000000000000000
#define EPS 1e-12

/* Special functions:
		find_by_order(k) --> returns iterator to the kth largest element counting from 0
		order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/

typedef tree<
ll,					// type long long
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

struct compare
{
    bool operator() (ll a, ll b)
    {
        return a>b;
    }
};

const ll p1=131,p2=137;
const ll mod1=1000000009;
const ll mod2=1000000007;

bool marked[MX];
vll primes;

ll bigMod(ll x, ll n)
{
    if(!n)
        return 1LL;
    ll temp=bigMod(x,n/2);
    temp=(temp*temp)%mod;
    if(n%2)
        temp=(temp*x)%mod;
    return temp;
}

void sieve()
{
    marked[0]=marked[1]=true;
    for(ll i=2;i*i<MX;i++){
        if(marked[i]==false){
            for(ll j=i*i;j<MX;j+=i){
                marked[j]=true;
            }
        }
    }

    for(ll i=2;i<MX;i++)
        if(!marked[i]){
            primes.push_back(i);
        }
}

vpl primeFactors(ll N)
{
	vpl factors;
	ll pf_id=0,pf=primes[pf_id];
	while(pf*pf<=N){
        ll cnt=0;
		while(N%pf==0) N/=pf,cnt++;
        if(cnt) factors.push_back({pf,cnt});

		pf=primes[++pf_id];
	}

	if(N!=1) factors.push_back({N,1});
    return factors;
}

struct pair_hash {
    template <class T1, class T2>
    size_t operator () (const pair<T1,T2> &p) const {
        auto h1 = hash<T1>{}(p.first);
        auto h2 = hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};

struct chash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	// Note: when casting y to unsigned x, x will be least unsigned int congruent
	// to y mod 2^64.
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
gp_hash_table<ll, ll, chash>var;

                        /**---------End of Template---------**/

ll cnt[MX];
ll vcnt[MX];

int main()
{

    FIO;

    ll n;

    cin>> n;
    vll v(n);
    for(ll i=0;i<n;i++) cin>> v[i];
    ll ans=0;
    for(ll i=0;i<n;i++){
        cnt[v[i]]++;
        for(ll j=0;j<=10;j++){
            if(cnt[j]){
                cnt[j]--;
                set<ll>st;
                for(ll k=0;k<=10;k++)
                    if(cnt[k]) st.insert(cnt[k]);
                if(st.size()==1) ans=i+1;
                cnt[j]++;
            }
        }
    }

    cout<< ans;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<< #x << " --> " << x << "\n";
#define NL printf("\n");
#define case(x) printf("Case %lld: ",x);
#define readI(x) scanf("%d",&x);
#define readL(x) scanf("%lld",&x);
#define writeI(x) printf("%d",x);
#define writeL(X) printf("%lld",x);
#define all(v) v.begin(),v.end()

#define ll long long
#define ld long double
#define pb push_back
#define pii pair< int,int >
#define pll pair< ll,ll >
#define vii vector< int >
#define vll vector< ll >
#define vss vector< string >
#define vdd vector< double >
#define vpi vector< pii >
#define vpl vector< pll >
#define vvi vector< vii >
#define vvl vector< vll >
#define PQ priority_queue
#define ff first
#define ss second
#define MX 2000005
#define mod 1000000007
#define INF 10000000000000000
#define EPS 1e-12

/* Special functions:
		find_by_order(k) --> returns iterator to the kth largest element counting from 0
		order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/

typedef tree<
ll,					// type long long
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

struct compare
{
    bool operator() (ll a, ll b)
    {
        return a>b;
    }
};

const ll p1=131,p2=137;
const ll mod1=1000000009;
const ll mod2=1000000007;

bool marked[MX];
vll primes;

ll bigMod(ll x, ll n)
{
    if(!n)
        return 1LL;
    ll temp=bigMod(x,n/2);
    temp=(temp*temp)%mod;
    if(n%2)
        temp=(temp*x)%mod;
    return temp;
}

void sieve()
{
    marked[0]=marked[1]=true;
    for(ll i=2;i*i<MX;i++){
        if(marked[i]==false){
            for(ll j=i*i;j<MX;j+=i){
                marked[j]=true;
            }
        }
    }

    for(ll i=2;i<MX;i++)
        if(!marked[i]){
            primes.push_back(i);
        }
}

vpl primeFactors(ll N)
{
	vpl factors;
	ll pf_id=0,pf=primes[pf_id];
	while(pf*pf<=N){
        ll cnt=0;
		while(N%pf==0) N/=pf,cnt++;
        if(cnt) factors.push_back({pf,cnt});

		pf=primes[++pf_id];
	}

	if(N!=1) factors.push_back({N,1});
    return factors;
}

struct pair_hash {
    template <class T1, class T2>
    size_t operator () (const pair<T1,T2> &p) const {
        auto h1 = hash<T1>{}(p.first);
        auto h2 = hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};

struct chash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	// Note: when casting y to unsigned x, x will be least unsigned int congruent
	// to y mod 2^64.
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
gp_hash_table<ll, ll, chash>var;

                        /**---------End of Template---------**/

ll cnt[MX];
ll vcnt[MX];

int main()
{

    FIO;

    ll n;

    cin>> n;
    vll v(n);
    for(ll i=0;i<n;i++) cin>> v[i];
    ll ans=0;
    for(ll i=0;i<n;i++){
        cnt[v[i]]++;
        for(ll j=0;j<=10;j++){
            if(cnt[j]){
                cnt[j]--;
                set<ll>st;
                for(ll k=0;k<=10;k++)
                    if(cnt[k]) st.insert(cnt[k]);
                if(st.size()==1 or st.size()==0) ans=i+1;
                cnt[j]++;
            }
        }
    }

    cout<< ans;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<< #x << " --> " << x << "\n";
#define NL printf("\n");
#define case(x) printf("Case %lld: ",x);
#define readI(x) scanf("%d",&x);
#define readL(x) scanf("%lld",&x);
#define writeI(x) printf("%d",x);
#define writeL(X) printf("%lld",x);
#define all(v) v.begin(),v.end()

#define ll long long
#define ld long double
#define pb push_back
#define pii pair< int,int >
#define pll pair< ll,ll >
#define vii vector< int >
#define vll vector< ll >
#define vss vector< string >
#define vdd vector< double >
#define vpi vector< pii >
#define vpl vector< pll >
#define vvi vector< vii >
#define vvl vector< vll >
#define PQ priority_queue
#define ff first
#define ss second
#define MX 100005
#define mod 1000000007
#define INF 10000000000000000
#define EPS 1e-12

/* Special functions:
		find_by_order(k) --> returns iterator to the kth largest element counting from 0
		order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/

typedef tree<
ll,					// type long long
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

struct compare
{
    bool operator() (ll a, ll b)
    {
        return a>b;
    }
};

const ll p1=131,p2=137;
const ll mod1=1000000009;
const ll mod2=1000000007;

bool marked[MX];
vll primes;

ll bigMod(ll x, ll n)
{
    if(!n)
        return 1LL;
    ll temp=bigMod(x,n/2);
    temp=(temp*temp)%mod;
    if(n%2)
        temp=(temp*x)%mod;
    return temp;
}

void sieve()
{
    marked[0]=marked[1]=true;
    for(ll i=2;i*i<MX;i++){
        if(marked[i]==false){
            for(ll j=i*i;j<MX;j+=i){
                marked[j]=true;
            }
        }
    }

    for(ll i=2;i<MX;i++)
        if(!marked[i]){
            primes.push_back(i);
        }
}

vpl primeFactors(ll N)
{
	vpl factors;
	ll pf_id=0,pf=primes[pf_id];
	while(pf*pf<=N){
        ll cnt=0;
		while(N%pf==0) N/=pf,cnt++;
        if(cnt) factors.push_back({pf,cnt});

		pf=primes[++pf_id];
	}

	if(N!=1) factors.push_back({N,1});
    return factors;
}

struct pair_hash {
    template <class T1, class T2>
    size_t operator () (const pair<T1,T2> &p) const {
        auto h1 = hash<T1>{}(p.first);
        auto h2 = hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};

struct chash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	// Note: when casting y to unsigned x, x will be least unsigned int congruent
	// to y mod 2^64.
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
gp_hash_table<ll, ll, chash>var;

                        /**---------End of Template---------**/

ll cnt[MX];
ll vcnt[MX];

int main()
{

    FIO;

    ll n;

    cin>> n;
    vll v(n);
    for(ll i=0;i<n;i++) cin>> v[i];

    ll ans=0;
    for(ll i=0;i<n;i++){
        cnt[v[i]]++;
        set<ll>st;
        for(ll j=1;j<=10;j++){
            if(cnt[j])
                st.insert(cnt[j]);
        }
        if(st.size()==1 and *st.begin()==1) ans=i+1;
        if(st.size()==2 and (abs(*st.begin()-*st.rbegin())==1 or *st.begin()==1 or *st.rbegin()==1)) ans=i+1;
    }

    cout<< ans;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<< #x << " --> " << x << "\n";
#define NL printf("\n");
#define case(x) printf("Case %lld: ",x);
#define readI(x) scanf("%d",&x);
#define readL(x) scanf("%lld",&x);
#define writeI(x) printf("%d",x);
#define writeL(X) printf("%lld",x);
#define all(v) v.begin(),v.end()

#define ll long long
#define ld long double
#define pb push_back
#define pii pair< int,int >
#define pll pair< ll,ll >
#define vii vector< int >
#define vll vector< ll >
#define vss vector< string >
#define vdd vector< double >
#define vpi vector< pii >
#define vpl vector< pll >
#define vvi vector< vii >
#define vvl vector< vll >
#define PQ priority_queue
#define ff first
#define ss second
#define MX 100005
#define mod 1000000007
#define INF 10000000000000000
#define EPS 1e-12

/* Special functions:
		find_by_order(k) --> returns iterator to the kth largest element counting from 0
		order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/

typedef tree<
ll,					// type long long
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

struct compare
{
    bool operator() (ll a, ll b)
    {
        return a>b;
    }
};

const ll p1=131,p2=137;
const ll mod1=1000000009;
const ll mod2=1000000007;

bool marked[MX];
vll primes;

ll bigMod(ll x, ll n)
{
    if(!n)
        return 1LL;
    ll temp=bigMod(x,n/2);
    temp=(temp*temp)%mod;
    if(n%2)
        temp=(temp*x)%mod;
    return temp;
}

void sieve()
{
    marked[0]=marked[1]=true;
    for(ll i=2;i*i<MX;i++){
        if(marked[i]==false){
            for(ll j=i*i;j<MX;j+=i){
                marked[j]=true;
            }
        }
    }

    for(ll i=2;i<MX;i++)
        if(!marked[i]){
            primes.push_back(i);
        }
}

vpl primeFactors(ll N)
{
	vpl factors;
	ll pf_id=0,pf=primes[pf_id];
	while(pf*pf<=N){
        ll cnt=0;
		while(N%pf==0) N/=pf,cnt++;
        if(cnt) factors.push_back({pf,cnt});

		pf=primes[++pf_id];
	}

	if(N!=1) factors.push_back({N,1});
    return factors;
}

struct pair_hash {
    template <class T1, class T2>
    size_t operator () (const pair<T1,T2> &p) const {
        auto h1 = hash<T1>{}(p.first);
        auto h2 = hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};

struct chash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	// Note: when casting y to unsigned x, x will be least unsigned int congruent
	// to y mod 2^64.
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
gp_hash_table<ll, ll, chash>var;

                        /**---------End of Template---------**/

ll cnt[MX];
ll vcnt[MX];

int main()
{

    FIO;

    ll n;

    cin>> n;
    vll v(n);
    set<ll>cc;
    for(ll i=0;i<n;i++) cin>> v[i],cc.insert(v[i]);
    vcnt[0]=cc.size();
    set<ll>st;

    ll ans=0;
    for(ll i=0;i<n;i++){
        st.insert(v[i]);
        vcnt[cnt[v[i]]]--;
        cnt[v[i]]++;
        vcnt[cnt[v[i]]]++;

        if(st.size()==1){
            ans=i+1;
            continue;
        }

        if(vcnt[1]==st.size()){
            ans=i+1;
            continue;
        }

        if(vcnt[cnt[v[i]]]==st.size()-1) {
            ll temp=(st.size()-1)*cnt[v[i]];
            ll x=i+1-temp;
            if(x-1==0 or x-1==cnt[v[i]])
                ans=i+1;
        }
        else{
            ll temp=-1;
            for(auto x:st){
                if(x!=v[i]){
                    temp=x;
                    break;
                }
            }
            if(vcnt[cnt[temp]]==st.size()-1){
                ll t=temp;
                temp=(st.size()-1)*cnt[t];
                ll x=i+1-temp;
                if(x-1==0 or x-1==cnt[t])
                    ans=i+1;
            }
        }
    }

    cout<< ans;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace __gnu_pbds;
using namespace __gnu_cxx;

#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<< #x << " --> " << x << "\n";
#define NL printf("\n");
#define case(x) printf("Case %lld: ",x);
#define readI(x) scanf("%d",&x);
#define readL(x) scanf("%lld",&x);
#define writeI(x) printf("%d",x);
#define writeL(X) printf("%lld",x);
#define all(v) v.begin(),v.end()

#define ll long long
#define ld long double
#define pb push_back
#define pii pair< int,int >
#define pll pair< ll,ll >
#define vii vector< int >
#define vll vector< ll >
#define vss vector< string >
#define vdd vector< double >
#define vpi vector< pii >
#define vpl vector< pll >
#define vvi vector< vii >
#define vvl vector< vll >
#define PQ priority_queue
#define ff first
#define ss second
#define MX 2000005
#define mod 1000000007
#define INF 10000000000000000
#define EPS 1e-12

/* Special functions:
		find_by_order(k) --> returns iterator to the kth largest element counting from 0
		order_of_key(val) --> returns the number of items in a set that are strictly smaller than our item
*/

typedef tree<
ll,					// type long long
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

struct compare
{
    bool operator() (ll a, ll b)
    {
        return a>b;
    }
};

const ll p1=131,p2=137;
const ll mod1=1000000009;
const ll mod2=1000000007;

bool marked[MX];
vll primes;

ll bigMod(ll x, ll n)
{
    if(!n)
        return 1LL;
    ll temp=bigMod(x,n/2);
    temp=(temp*temp)%mod;
    if(n%2)
        temp=(temp*x)%mod;
    return temp;
}

void sieve()
{
    marked[0]=marked[1]=true;
    for(ll i=2;i*i<MX;i++){
        if(marked[i]==false){
            for(ll j=i*i;j<MX;j+=i){
                marked[j]=true;
            }
        }
    }

    for(ll i=2;i<MX;i++)
        if(!marked[i]){
            primes.push_back(i);
        }
}

vpl primeFactors(ll N)
{
	vpl factors;
	ll pf_id=0,pf=primes[pf_id];
	while(pf*pf<=N){
        ll cnt=0;
		while(N%pf==0) N/=pf,cnt++;
        if(cnt) factors.push_back({pf,cnt});

		pf=primes[++pf_id];
	}

	if(N!=1) factors.push_back({N,1});
    return factors;
}

struct pair_hash {
    template <class T1, class T2>
    size_t operator () (const pair<T1,T2> &p) const {
        auto h1 = hash<T1>{}(p.first);
        auto h2 = hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};

struct chash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
	// Note: when casting y to unsigned x, x will be least unsigned int congruent
	// to y mod 2^64.
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
gp_hash_table<ll, ll, chash>var;

                        /**---------End of Template---------**/

ll cnt[MX];
ll vcnt[MX];

int main()
{

    FIO;

    ll n;

    cin>> n;
    vll v(n);
    set<ll>cc;
    for(ll i=0;i<n;i++) cin>> v[i],cc.insert(v[i]);
    vcnt[0]=cc.size();
    set<ll>st;

    ll ans=0;
    for(ll i=0;i<n;i++){
        st.insert(v[i]);
        vcnt[cnt[v[i]]]--;
        cnt[v[i]]++;
        vcnt[cnt[v[i]]]++;

        if(st.size()==1){
            ans=i+1;
            continue;
        }

        if(vcnt[1]==st.size()){
            ans=i+1;
            continue;
        }

        if(vcnt[cnt[v[i]]]==st.size()-1) {
            ll temp=(st.size()-1)*cnt[v[i]];
            ll x=i+1-temp;
            if(x-1==0 or x-1==cnt[v[i]])
                ans=i+1;
        }
        else{
            ll temp=-1;
            for(auto x:st){
                if(x!=v[i]){
                    temp=x;
                    break;
                }
            }
            if(vcnt[cnt[temp]]==st.size()-1){
                ll t=temp;
                temp=(st.size()-1)*cnt[t];
                ll x=i+1-temp;
                if(x-1==0 or x-1==cnt[t])
                    ans=i+1;
            }
        }
    }

    cout<< ans;
}


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb(x) push_back(x)
#define ins(x) insert(x)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) (v.begin(),v.end())
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define rep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define irep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef  long long int   ll;
const long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,-1};
ll m,k,n,a[500005],res=0,ans=0,sm=0,vis[300005];
string s1="",s,ch="";
vector<ll>v,vnode[300005],v1,v2,v3;
vector< pair<ll,ll> >vv[300005];
vector< pair< ll,pair <ll,ll> > > vvv;
priority_queue <ll> pq;
set<pair<ll,ll> >st1,st2;
map<ll,ll >red,red2;
set< pair<ll,ll > > ::iterator it,it1,it2;
ll xa,xb,xc,ya,yb,yc,xres,yres;
set<ll>st;
int main(){
In_the_name_of_allah_the_merciful
	ll i=0,x,y,j=0,z=0,q,nb0=0,nb=0,nbmax=0,nbinf=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0,l1,r1;
	char c;
  // freopen("a.in","r",stdin);
   cin>>n;
   for(i=1;i<=n;i++){
       cin>>a[i];
       if(!red[a[i]])v.pb(a[i]);
       red[a[i]]++;
     }
    for(i=0;i<v.size();i++){
        red2[red[v[i]]]++;
        st.insert(red[v[i]]);
        st1.insert({red[v[i]],v[i]});
        st2.insert({-red[v[i]],v[i]});
    }
    for(i=n;i>=1;i--){
        it1=st1.begin();
        ll x1=(*it1).first;
        it2=st2.begin();
        ll x2=(*it2).first;
        x2=abs(x2);
       // cout<<i<<" "<<x1<<" "<<x2<<endl;

        if(x1==x2){
            if(i<n)return cout<<(i+1),0;
            //else return cout<<(i),0;
        }
        if(st.size()==2){
           if(red2[x2]==1&&x1==(x2-1)||red2[x1]==1&&x1==1)return cout<<i,0;

        }

        x=a[i];y=red[a[i]];
       // cout<<x<<" fdvdf\n";
       st.erase(y);
        red2[red[a[i]]]--;
        red[a[i]]--;
        red2[red[a[i]]]++;
        st1.erase({y,x});
        st2.erase({-y,x});
        y--;
        if(!y)continue;
        st.insert(y);
        st1.insert({y,x});
        st2.insert({-y,x});
    }
    cout<<"1";
Alhamdulillah
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb(x) push_back(x)
#define ins(x) insert(x)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) (v.begin(),v.end())
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define rep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define irep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef  long long int   ll;
const long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,-1};
ll m,k,n,a[500005],res=0,ans=0,sm=0,vis[300005];
string s1="",s,ch="";
vector<ll>v,vnode[300005],v1,v2,v3;
vector< pair<ll,ll> >vv[300005];
vector< pair< ll,pair <ll,ll> > > vvv;
priority_queue <ll> pq;
set<pair<ll,ll> >st1,st2;
map<ll,ll >red,red2;
set< pair<ll,ll > > ::iterator it,it1,it2;
ll xa,xb,xc,ya,yb,yc,xres,yres;
ll prime[200005];

int main(){
In_the_name_of_allah_the_merciful
	ll i=0,x,y,j=0,z=0,q,nb0=0,nb=0,nbmax=0,nbinf=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0,l1,r1;
	char c;
   // freopen("a.in","r",stdin);
   cin>>n;
   for(i=1;i<=n;i++){
       cin>>a[i];
       if(!red[a[i]])v.pb(a[i]);
       red[a[i]]++;
     }
    for(i=0;i<v.size();i++){
        red2[red[v[i]]]++;
        st1.insert({red[v[i]],v[i]});
        st2.insert({-red[v[i]],v[i]});
    }
    for(i=n;i>=1;i--){
        it1=st1.begin();
        ll x1=(*it1).first;
        it2=st2.begin();
        ll x2=(*it2).first;
        x2=abs(x2);
        //cout<<i<<" "<<x1<<" "<<x2<<endl;

        if(x1==x2){
            if(i<n)return cout<<(i+1),0;
            //else return cout<<(i),0;
        }
        if(x1==(x2-1)){
           if(red2[x2]==1)return cout<<i,0;
        }
        x=a[i];y=red[a[i]];
       // cout<<x<<" fdvdf\n";
        red2[red[a[i]]]--;
        red[a[i]]--;
        red2[red[a[i]]]++;
        st1.erase({y,x});
        st2.erase({-y,x});
        y--;
        if(!y)continue;
        st1.insert({y,x});
        st2.insert({-y,x});
    }
    cout<<"0";
Alhamdulillah
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb(x) push_back(x)
#define ins(x) insert(x)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) (v.begin(),v.end())
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define rep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define irep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef  long long int   ll;
const long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,-1};
ll m,k,n,a[500005],res=0,ans=0,sm=0,vis[300005];
string s1="",s,ch="";
vector<ll>v,vnode[300005],v1,v2,v3;
vector< pair<ll,ll> >vv[300005];
vector< pair< ll,pair <ll,ll> > > vvv;
priority_queue <ll> pq;
set<pair<ll,ll> >st1,st2;
map<ll,ll >red,red2;
set< pair<ll,ll > > ::iterator it,it1,it2;
ll xa,xb,xc,ya,yb,yc,xres,yres;
ll prime[200005];

int main(){
In_the_name_of_allah_the_merciful
	ll i=0,x,y,j=0,z=0,q,nb0=0,nb=0,nbmax=0,nbinf=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0,l1,r1;
	char c;
 //   freopen("a.in","r",stdin);
   cin>>n;
   for(i=1;i<=n;i++){
       cin>>a[i];
       if(!red[a[i]])v.pb(a[i]);
       red[a[i]]++;
     }
    for(i=0;i<v.size();i++){
        red2[red[v[i]]]++;
        st1.insert({red[v[i]],v[i]});
        st2.insert({-red[v[i]],v[i]});
    }
    for(i=n;i>=1;i--){
        it1=st1.begin();
        ll x1=(*it1).first;
        it2=st2.begin();
        ll x2=(*it2).first;
        x2=abs(x2);
        //cout<<i<<" "<<x1<<" "<<x2<<endl;

        if(x1==x2){
            if(i<n)return cout<<(i+1),0;
            //else return cout<<(i),0;
        }
        if(x1==(x2-1)){
           if(red2[x2]==1)return cout<<i,0;
        }
        x=a[i];y=red[a[i]];
       // cout<<x<<" fdvdf\n";
        red2[red[a[i]]]--;
        red[a[i]]--;
        red2[red[a[i]]]++;
        st1.erase({y,x});
        st2.erase({-y,x});
        y--;
        if(!y)continue;
        st1.insert({y,x});
        st2.insert({-y,x});
    }
    cout<<"1";
Alhamdulillah
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb(x) push_back(x)
#define ins(x) insert(x)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) (v.begin(),v.end())
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define rep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define irep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef  long long int   ll;
const long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,-1};
ll m,k,n,a[500005],res=0,ans=0,sm=0,vis[300005];
string s1="",s,ch="";
vector<ll>v,vnode[300005],v1,v2,v3;
vector< pair<ll,ll> >vv[300005];
vector< pair< ll,pair <ll,ll> > > vvv;
priority_queue <ll> pq;
set<pair<ll,ll> >st1,st2;
map<ll,ll >red,red2;
set< pair<ll,ll > > ::iterator it,it1,it2;
ll xa,xb,xc,ya,yb,yc,xres,yres;
ll prime[200005];

int main(){
In_the_name_of_allah_the_merciful
	ll i=0,x,y,j=0,z=0,q,nb0=0,nb=0,nbmax=0,nbinf=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0,l1,r1;
	char c;
   // freopen("a.in","r",stdin);
   cin>>n;
   for(i=1;i<=n;i++){
       cin>>a[i];
       if(!red[a[i]])v.pb(a[i]);
       red[a[i]]++;
     }
    for(i=0;i<v.size();i++){
        red2[red[v[i]]]++;
        st1.insert({red[v[i]],v[i]});
        st2.insert({-red[v[i]],v[i]});
    }
    for(i=n;i>=1;i--){
        it1=st1.begin();
        ll x1=(*it1).first;
        it2=st2.begin();
        ll x2=(*it2).first;
        x2=abs(x2);
        //cout<<i<<" "<<x1<<" "<<x2<<endl;

        if(x1==x2){
            if(i<n)return cout<<(i+1),0;
            else return cout<<(i),0;
        }
        if(x1==(x2-1)){
           if(red2[x2]==1)return cout<<i,0;
        }
        x=a[i];y=red[a[i]];
       // cout<<x<<" fdvdf\n";
        red2[red[a[i]]]--;
        red[a[i]]--;
        red2[red[a[i]]]++;
        st1.erase({y,x});
        st2.erase({-y,x});
        y--;
        if(!y)continue;
        st1.insert({y,x});
        st2.insert({-y,x});
    }
    cout<<"0";
Alhamdulillah
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb(x) push_back(x)
#define ins(x) insert(x)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) (v.begin(),v.end())
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define rep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define irep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef  long long int   ll;
const long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,-1};
ll m,k,n,a[500005],res=0,ans=0,sm=0,vis[300005];
string s1="",s,ch="";
vector<ll>v,vnode[300005],v1,v2,v3;
vector< pair<ll,ll> >vv[300005];
vector< pair< ll,pair <ll,ll> > > vvv;
priority_queue <ll> pq;
set<pair<ll,ll> >st1,st2;
map<ll,ll >red,red2;
set< pair<ll,ll > > ::iterator it,it1,it2;
ll xa,xb,xc,ya,yb,yc,xres,yres;
set<ll>st;
int main(){
In_the_name_of_allah_the_merciful
	ll i=0,x,y,j=0,z=0,q,nb0=0,nb=0,nbmax=0,nbinf=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0,l1,r1;
	char c;
  // freopen("a.in","r",stdin);
   cin>>n;
   for(i=1;i<=n;i++){
       cin>>a[i];
       if(!red[a[i]])v.pb(a[i]);
       red[a[i]]++;
     }
    for(i=0;i<v.size();i++){
        red2[red[v[i]]]++;
        st.insert(red[v[i]]);
        st1.insert({red[v[i]],v[i]});
        st2.insert({-red[v[i]],v[i]});
    }
    for(i=n;i>=1;i--){
        it1=st1.begin();
        ll x1=(*it1).first;
        it2=st2.begin();
        ll x2=(*it2).first;
        x2=abs(x2);
       // cout<<i<<" "<<x1<<" "<<x2<<endl;

        if(x1==x2){
            if(i<n)return cout<<(i+1),0;
            //else return cout<<(i),0;
        }
        if(st.size()==2){
           if(red2[x1]==1||red2[x2]==1)return cout<<i,0;
        }

        x=a[i];y=red[a[i]];
       // cout<<x<<" fdvdf\n";
       st.erase(y);
        red2[red[a[i]]]--;
        red[a[i]]--;
        red2[red[a[i]]]++;
        st1.erase({y,x});
        st2.erase({-y,x});
        y--;
        if(!y)continue;
        st.insert(y);
        st1.insert({y,x});
        st2.insert({-y,x});
    }
    cout<<"1";
Alhamdulillah
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb(x) push_back(x)
#define ins(x) insert(x)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) (v.begin(),v.end())
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define rep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define irep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef  long long int   ll;
const long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,-1};
ll m,k,n,a[500005],res=0,ans=0,sm=0,vis[300005];
string s1="",s,ch="";
vector<ll>v,vnode[300005],v1,v2,v3;
vector< pair<ll,ll> >vv[300005];
vector< pair< ll,pair <ll,ll> > > vvv;
priority_queue <ll> pq;
set<pair<ll,ll> >st1,st2;
map<ll,ll >red,red2;
set< pair<ll,ll > > ::iterator it,it1,it2;
ll xa,xb,xc,ya,yb,yc,xres,yres;

int main(){
In_the_name_of_allah_the_merciful
	ll i=0,x,y,j=0,z=0,q,nb0=0,nb=0,nbmax=0,nbinf=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0,l1,r1;
	char c;
   //freopen("a.in","r",stdin);
   cin>>n;
   for(i=1;i<=n;i++){
       cin>>a[i];
       if(!red[a[i]])v.pb(a[i]);
       red[a[i]]++;
     }
    for(i=0;i<v.size();i++){
        red2[red[v[i]]]++;
        st1.insert({red[v[i]],v[i]});
        st2.insert({-red[v[i]],v[i]});
    }
    for(i=n;i>=1;i--){
        it1=st1.begin();
        ll x1=(*it1).first;
        it2=st2.begin();
        ll x2=(*it2).first;
        x2=abs(x2);
       // cout<<i<<" "<<x1<<" "<<x2<<endl;

        if(x1==x2){
            if(i<n)return cout<<(i+1),0;
            //else return cout<<(i),0;
        }
        if(x1==(x2-1)){
           if(red2[x2]==1||red2[x1]==1)return cout<<i,0;
        }
        x=a[i];y=red[a[i]];
       // cout<<x<<" fdvdf\n";
        red2[red[a[i]]]--;
        red[a[i]]--;
        red2[red[a[i]]]++;
        st1.erase({y,x});
        st2.erase({-y,x});
        y--;
        if(!y)continue;
        st1.insert({y,x});
        st2.insert({-y,x});
    }
    cout<<"1";
Alhamdulillah
 }


#include<bits/stdc++.h>
#define In_the_name_of_allah_the_merciful ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define Alhamdulillah return 0;
#define pb(x) push_back(x)
#define ins(x) insert(x)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) (v.begin(),v.end())
#define sz(v) ((ll)((v).size()))
#define reset(v,val) memset(v,val,sizeof(v))
#define rep(i, x, n) for ( i = x; i<=(ll)(n); i++)
#define irep(i, x, n) for( i = x; i<(ll)(n); i++)
using namespace std;
typedef  long long int   ll;
const long long int mod=1e9+7;
ll dx[]={0,0,1,-1};
ll dy[]={1,-1,0,-1};
ll m,k,n,a[500005],res=0,ans=0,sm=0,vis[300005];
string s1="",s,ch="";
vector<ll>v,vnode[300005],v1,v2,v3;
vector< pair<ll,ll> >vv[300005];
vector< pair< ll,pair <ll,ll> > > vvv;
priority_queue <ll> pq;
set<pair<ll,ll> >st1,st2;
map<ll,ll >red,red2;
set< pair<ll,ll > > ::iterator it,it1,it2;
ll xa,xb,xc,ya,yb,yc,xres,yres;
set<ll>st;
int main(){
In_the_name_of_allah_the_merciful
	ll i=0,x,y,j=0,z=0,q,nb0=0,nb=0,nbmax=0,nbinf=0,nb2=0,mm=5e8,idx=0,ok=0,l,r,negatif=0,positif=0,l1,r1;
	char c;
 // freopen("a.in","r",stdin);
   cin>>n;
   for(i=1;i<=n;i++){
       cin>>a[i];
       if(!red[a[i]])v.pb(a[i]);
       red[a[i]]++;
     }
    for(i=0;i<v.size();i++){
        red2[red[v[i]]]++;
        st.insert(red[v[i]]);
        st1.insert({red[v[i]],v[i]});
        st2.insert({-red[v[i]],v[i]});
    }
    for(i=n;i>=1;i--){
        it1=st1.begin();
        ll x1=(*it1).first;
        it2=st2.begin();
        ll x2=(*it2).first;
        x2=abs(x2);
       // cout<<i<<" "<<x1<<" "<<x2<<endl;

        if(x1==x2){
            if(i<n)return cout<<(i+1),0;
            //else return cout<<(i),0;
        }
        if(st.size()==2){
               // cout<<i<<" "<<x1<<" "<<x2<<endl;
           if(red2[x2]==1&&x1==(x2-1)||red2[x1]==1&&x1==1)return cout<<i,0;

        }

        x=a[i];y=red[a[i]];
       // cout<<x<<" fdvdf\n";
       st.erase(y);
        red2[red[a[i]]]--;
        red[a[i]]--;
        if(red2[y])st.insert(y);
        red2[red[a[i]]]++;
        st1.erase({y,x});
        st2.erase({-y,x});
        y--;
        if(!y)continue;
        st.insert(y);
        st1.insert({y,x});
        st2.insert({-y,x});
    }
    cout<<"1";
Alhamdulillah
 }


#include <bits/stdc++.h>

using namespace std;

int n;
const int maxn = 100009;
const int maxc = 19;
int a[maxn][maxc];

void Enter()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= 10; j++)
            a[i][j] = a[i-1][j];
        int k;
        cin >> k;
        a[i][k]++;
    }
}

void Solve()
{
    int t[11], p[11], s[11];
    for (int i = n; i >= 1; i--)
    {
        int spt = 0;
        for (int j = 1; j <= 10; j++)
            if (a[i][j] > 0)
                t[++spt] = a[i][j];
        sort(t + 1, t + spt + 1);
        int dem = 1;
        bool kt = true;
        p[1] = t[1];
        s[1] = 1;
        for (int j = 2; j <= spt; j++)
            if (p[dem] != t[j])
                p[++dem] = t[j], s[dem] = 1;
            else
                s[dem]++;
        if (spt == 1)
        {
            cout << n;
            return;
        }
        }
        if (dem == 2)
            if ((p[2] - p[1] == 1 && s[2] == 1) || (s[1] == 1 && p[1] == 1))
            {
                cout << i;
                return;
            }
        if (dem == 1 && p[1] == 1)
        {
            cout << i;
            return;
        }
    }
}

int main()
{
    Enter();
    if (n == 1 || n == 2)
    {
        if (n == 1)
            cout << 1;
        else
            cout << 2;
    }
    else
        Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
const int maxn = 100009;
const int maxc = 19;
int a[maxn][maxc];

void Enter()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= 10; j++)
            a[i][j] = a[i-1][j];
        int k;
        cin >> k;
        a[i][k]++;
    }
}

void Solve()
{
    int t[11], p[11], s[11];
    for (int i = n; i >= 1; i--)
    {
        int spt = 0;
        for (int j = 1; j <= 10; j++)
            if (a[i][j] > 0)
                t[++spt] = a[i][j];
        sort(t + 1, t + spt + 1);
        int dem = 1;
        bool kt = true;
        p[1] = t[1];
        s[1] = 1;
        for (int j = 2; j <= spt; j++)
            if (p[dem] != t[j])
                p[++dem] = t[j], s[dem] = 1;
            else
                s[dem]++;
        if (dem == 2)
            if ((p[2] - p[1] == 1 && s[2] == 1) || (s[1] == 1 && p[1] == 1))
            {
                cout << i;
                return;
            }
        if (dem == 1)
        {
            cout << i;
            return;
        }
    }
}

int main()
{
    Enter();
    if (n == 1 || n == 2)
    {
        if (n == 1)
            cout << 1;
        else
            cout << 2;
    }
    else
        Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
const int maxn = 100009;
const int maxc = 19;
int a[maxn][maxc];

void Enter()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= 10; j++)
            a[i][j] = a[i-1][j];
        int k;
        cin >> k;
        a[i][k]++;
    }
}

void Solve()
{
    int t[11], p[11], s[11];
    for (int i = n; i >= 1; i--)
    {
        int spt = 0;
        for (int j = 1; j <= 10; j++)
            if (a[i][j] > 0)
                t[++spt] = a[i][j];
        sort(t + 1, t + spt + 1);
        int dem = 1;
        bool kt = true;
        p[1] = t[1];
        s[1] = 1;
        for (int j = 2; j <= spt; j++)
            if (p[dem] != t[j])
                p[++dem] = t[j], s[dem] = 1;
            else
                s[dem]++;
        if (dem == 2)
            if ((p[2] - p[1] == 1 && s[2] == 1) || (s[1] == 1 && p[1] == 1))
            {
                cout << i;
                return;
            }
    }
}

int main()
{
    Enter();
    if (n == 1 || n == 2)
    {
        if (n == 1)
            cout << 1;
        else
            cout << 2;
    }
    else
        Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
const int maxn = 100009;
const int maxc = 19;
int a[maxn][maxc];
struct obj
{
    int gt, sl;
};

void Enter()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= 10; j++)
            a[i][j] = a[i-1][j];
        int k;
        cin >> k;
        a[i][k]++;
    }
}

void Solve()
{
    for (int i = n; i >= 1; i--)
    {
        int t[maxc], tsize = 0, ksize = 0;
        obj k[maxc];
        for (int j = 1; j <= 10; j++)
            if (a[i][j] > 0)
                t[++tsize] = a[i][j];
        sort(t + 1, t + tsize + 1);
        k[++ksize].gt = t[1], k[ksize].sl = 1;
        for (int j = 2; j <= tsize; j++)
            if (k[ksize].gt == t[j])
                k[ksize].sl++;
            else
            {
                k[++ksize].gt = t[j];
                k[ksize].sl = 1;
            }
        if (ksize == 1 && k[1].sl == 1)
        {
            cout << i;
            return;
        }
        if (ksize == 2 && ((k[1].gt == 1 && k[1].sl == 1) || (k[2].gt - k[1].gt == 1 && k[2].sl == 1)))
        {
            cout << i;
            return;
        }
    }
}

int main()
{
    Enter();
    Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
const int maxn = 100009;
const int maxc = 19;
int a[maxn][maxc];

void Enter()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= 10; j++)
            a[i][j] = a[i-1][j];
        int k;
        cin >> k;
        a[i][k]++;
    }
}

void Solve()
{
    int t[11], p[11], s[11];
    for (int i = n; i >= 1; i--)
    {
        int spt = 0;
        for (int j = 1; j <= 10; j++)
            if (a[i][j] > 0)
                t[++spt] = a[i][j];
        sort(t + 1, t + spt + 1);
        int dem = 1;
        bool kt = true;
        p[1] = t[1];
        s[1] = 1;
        for (int j = 2; j <= spt; j++)
            if (p[dem] != t[j])
                p[++dem] = t[j], s[dem] = 1;
            else
                s[dem]++;
        if (dem == 2)
            if ((p[2] - p[1] == 1 && s[2] == 1) || (s[1] == 1 && p[1] == 1))
            {
                cout << i;
                return;
            }
        if (dem == 1 && p[1] == 1)
        {
            cout << i;
            return;
        }
    }
}

int main()
{
    Enter();
    if (n == 1 || n == 2)
    {
        if (n == 1)
            cout << 1;
        else
            cout << 2;
    }
    else
        Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
const int maxn = 100009;
const int maxc = 19;
int a[maxn][maxc];

void Enter()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= 10; j++)
            a[i][j] = a[i-1][j];
        int k;
        cin >> k;
        a[i][k]++;
    }
}

void Solve()
{
    int t[11], p[11], s[11];
    for (int i = n; i >= 1; i--)
    {
        int spt = 0;
        for (int j = 1; j <= 10; j++)
            if (a[i][j] > 0)
                t[++spt] = a[i][j];
        sort(t + 1, t + spt + 1);
        int dem = 1;
        bool kt = true;
        p[1] = t[1];
        s[1] = 1;
        for (int j = 2; j <= spt; j++)
            if (p[dem] != t[j])
                p[++dem] = t[j], s[dem] = 1;
            else
                s[dem]++;
        if (spt == 1)
        {
            cout << n;
            return;
        }
        }
        if (dem == 2)
            if ((p[2] - p[1] == 1 && s[2] == 1) || (s[1] == 1 && p[1] == 1))
            {
                cout << i;
                return;
            }
        if (dem == 1 && p[1] == 1)
        {
            cout << i;
            return;
        }
    }
}

int main()
{
    Enter();
    if (n == 1 || n == 2)
    {
        if (n == 1)
            cout << 1;
        else
            cout << 2;
    }
    else
        Solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
const int maxn = 100009;
const int maxc = 19;
int a[maxn][maxc];
struct obj
{
    int gt, sl;
};

void Enter()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= 10; j++)
            a[i][j] = a[i-1][j];
        int k;
        cin >> k;
        a[i][k]++;
    }
}

void Solve()
{
    for (int i = n; i >= 1; i--)
    {
        int t[maxc], tsize = 0, ksize = 0;
        obj k[maxc];
        for (int j = 1; j <= 10; j++)
            if (a[i][j] > 0)
                t[++tsize] = a[i][j];
        sort(t + 1, t + tsize + 1);
        k[++ksize].gt = t[1], k[ksize].sl = 1;
        for (int j = 2; j <= tsize; j++)
            if (k[ksize].gt == t[j])
                k[ksize].sl++;
            else
            {
                k[++ksize].gt = t[j];
                k[ksize].sl = 1;
            }
        if (ksize == 1 && (k[1].sl == 1 || (k[1].gt == 1)))
        {
            cout << i;
            return;
        }
        if (ksize == 2 && ((k[1].gt == 1 && k[1].sl == 1) || (k[2].gt - k[1].gt == 1 && k[2].sl == 1)))
        {
            cout << i;
            return;
        }
    }
}

int main()
{
    Enter();
    Solve();
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int maxn=1e5+5;
int vis[maxn][15];
bool check(int idx)
{
	map<int,int>M;
	int num=-1;
	for(int i=1;i<=10;i++)
		if(vis[idx][i]==0)
			continue;
		else
		{
			//cout<<vis[idx][i]<<endl;
			M[vis[idx][i]]++;
		}
	if(M.size()==1)
	{
		return 1;
	}
	else if(M.size()==2)
	{
		int num=0;
		for(auto itor = M.begin();itor!=M.end();itor++)
		{
			num = itor->second;
			if(num==1)
				return 1;
		}
		//cout<<num<<endl
		return 0;
	}
	return 0;
}
int main()
{
	int n;
	scanf("%d",&n);
	int a;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a);
		for(int j=1;j<=10;j++)
			vis[i][j]=vis[i-1][j];
		vis[i][a]++;
	}
	// for(int i=1;i<=10;i++)
		// cout<<vis[6][i]<<endl;
	for(int i=n;i>=1;i--)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}
}
/*
(((())))()

*/


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int maxn=1e5+5;
int vis[maxn][15];
bool check(int idx)
{
	map<int,int>M;
	int num=-1;
	for(int i=1;i<=10;i++)
		if(vis[idx][i]==0)
			continue;
		else
		{
			//cout<<vis[idx][i]<<endl;
			M[vis[idx][i]]++;
		}
	if(M.size()==1)
	{
		auto itor = M.begin();
		if(itor->first==1)
			return 1;
		return 0;
	}
	else if(M.size()==2)
	{
		int num=0;
		int now=0;
		int f1[3],f2[3];
		for(auto itor = M.begin();itor!=M.end();itor++)
		{
			f1[now]=itor->first;
			f2[now++]=itor->second;
		}
		if(f1[0]<f1[1])
		{
			swap(f1[0],f1[1]);
			swap(f2[0],f2[1]);
		}
		if(f1[0]==f1[1]+1&&f1[1]==1)
			return 1;
		if(f1[1]==1&&f2[1]==1)
			return 1;
		return 0;
		//cout<<num<<endl
		return 0;
	}
	return 0;
}
int main()
{
	int n;
	scanf("%d",&n);
	int a;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a);
		for(int j=1;j<=10;j++)
			vis[i][j]=vis[i-1][j];
		vis[i][a]++;
	}
	// for(int i=1;i<=10;i++)
		// cout<<vis[6][i]<<endl;
	for(int i=n;i>=1;i--)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}
}
/*
(((())))()

*/


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int maxn=1e5+5;
int vis[maxn][15];
bool check(int idx)
{
	map<int,int>M;
	int num=-1;
	for(int i=1;i<=10;i++)
		if(vis[idx][i]==0)
			continue;
		else
		{
			//cout<<vis[idx][i]<<endl;
			M[vis[idx][i]]++;
		}
	if(M.size()==1)
	{
		auto itor = M.begin();
		if(itor->first==1)
			return 1;
		if(itor->second==1)
			return 1;
		return 0;
	}
	else if(M.size()==2)
	{
		int num=0;
		int now=0;
		int f1[3],f2[3];
		for(auto itor = M.begin();itor!=M.end();itor++)
		{
			f1[now]=itor->first;
			f2[now++]=itor->second;
		}
		if(f1[0]<f1[1])
		{
			swap(f1[0],f1[1]);
			swap(f2[0],f2[1]);
		}
		if(f1[0]==f1[1]+1&&f2[0]==1)
			return 1;
		if(f1[1]==1&&f2[1]==1)
			return 1;
		return 0;
		//cout<<num<<endl
		return 0;
	}
	return 0;
}
int main()
{
	int n;
	scanf("%d",&n);
	int a;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a);
		for(int j=1;j<=10;j++)
			vis[i][j]=vis[i-1][j];
		vis[i][a]++;
	}
	// for(int i=1;i<=10;i++)
		// cout<<vis[6][i]<<endl;
	for(int i=n;i>=1;i--)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}
}
/*
(((())))()

*/


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int maxn=1e5+5;
int vis[maxn][15];
bool check(int idx)
{
	map<int,int>M;
	int num=-1;
	for(int i=1;i<=10;i++)
		if(vis[idx][i]==0)
			continue;
		else
		{
			//cout<<vis[idx][i]<<endl;
			M[vis[idx][i]]++;
		}
	if(M.size()==1)
	{
		auto itor = M.begin();
		if(itor->first==1)
			return 1;
		return 0;
	}
	else if(M.size()==2)
	{
		int num=0;
		int now=0;
		int f1[3],f2[3];
		for(auto itor = M.begin();itor!=M.end();itor++)
		{
			f1[now]=itor->first;
			f2[now++]=itor->second;
		}
		if(f1[0]<f1[1])
		{
			swap(f1[0],f1[1]);
			swap(f2[0],f2[1]);
		}
		if(f1[0]==f1[1]+1&&f2[0]==1)
			return 1;
		if(f1[1]==1&&f2[1]==1)
			return 1;
		return 0;
		//cout<<num<<endl
		return 0;
	}
	return 0;
}
int main()
{
	int n;
	scanf("%d",&n);
	int a;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a);
		for(int j=1;j<=10;j++)
			vis[i][j]=vis[i-1][j];
		vis[i][a]++;
	}
	// for(int i=1;i<=10;i++)
		// cout<<vis[6][i]<<endl;
	for(int i=n;i>=1;i--)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}
}
/*
(((())))()

*/


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int maxn=1e5+5;
int vis[maxn][15];
bool check(int idx)
{
	map<int,int>M;
	int num=-1;
	for(int i=1;i<=10;i++)
		if(vis[idx][i]==0)
			continue;
		else
		{
			//cout<<vis[idx][i]<<endl;
			M[vis[idx][i]]=1;
		}
	if(M.size()==1)
	{
		return 1;
	}
	else if(M.size()==2)
	{
		int num=0;
		for(auto itor = M.begin();itor!=M.end();itor++)
		{
			if(num==0)
				num = itor->first;
			else num= abs(num-itor->first);
		}
		//cout<<num<<endl;
		if(num==1)return 1;
		return 0;
	}
	return 0;
}
int main()
{
	int n;
	scanf("%d",&n);
	int a;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a);
		for(int j=1;j<=10;j++)
			vis[i][j]=vis[i-1][j];
		vis[i][a]++;
	}
	// for(int i=1;i<=10;i++)
		// cout<<vis[6][i]<<endl;
	for(int i=n;i>=1;i--)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}
}
/*
(((())))()

*/


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int maxn=1e5+5;
int vis[maxn][15];
bool check(int idx)
{
	map<int,int>M;
	int num=-1;
	for(int i=1;i<=10;i++)
		if(vis[idx][i]==0)
			continue;
		else
		{
			//cout<<vis[idx][i]<<endl;
			M[vis[idx][i]]++;
		}
	if(M.size()==1)
	{
		return 1;
	}
	else if(M.size()==2)
	{
		int num=0;
		int now=0;
		int f1[3],f2[3];
		for(auto itor = M.begin();itor!=M.end();itor++)
		{
			f1[now]=itor->first;
			f2[now++]=itor->second;
		}
		if(f1[0]<f1[1])
		{
			swap(f1[0],f1[1]);
			swap(f2[0],f2[1]);
		}
		if(f1[0]==f1[1]+1&&f1[1]==1)
			return 1;
		if(f1[1]==1&&f2[1]==1)
			return 1;
		return 0;
		//cout<<num<<endl
		return 0;
	}
	return 0;
}
int main()
{
	int n;
	scanf("%d",&n);
	int a;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a);
		for(int j=1;j<=10;j++)
			vis[i][j]=vis[i-1][j];
		vis[i][a]++;
	}
	// for(int i=1;i<=10;i++)
		// cout<<vis[6][i]<<endl;
	for(int i=n;i>=1;i--)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}
}
/*
(((())))()

*/


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
const int maxn=1e5+5;
int vis[maxn][15];
bool check(int idx)
{
	map<int,int>M;
	int num=-1;
	for(int i=1;i<=10;i++)
		if(vis[idx][i]==0)
			continue;
		else
		{
			//cout<<vis[idx][i]<<endl;
			M[vis[idx][i]]++;
		}
	if(M.size()==1)
	{
		return 0;
	}
	else if(M.size()==2)
	{
		int num=0;
		int now=0;
		int f1[3],f2[3];
		for(auto itor = M.begin();itor!=M.end();itor++)
		{
			f1[now]=itor->first;
			f2[now++]=itor->second;
		}
		if(f1[0]<f1[1])
		{
			swap(f1[0],f1[1]);
			swap(f2[0],f2[1]);
		}
		if(f1[0]==f1[1]+1&&f1[1]==1)
			return 1;
		if(f1[1]==1&&f2[1]==1)
			return 1;
		return 0;
		//cout<<num<<endl
		return 0;
	}
	return 0;
}
int main()
{
	int n;
	scanf("%d",&n);
	int a;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a);
		for(int j=1;j<=10;j++)
			vis[i][j]=vis[i-1][j];
		vis[i][a]++;
	}
	// for(int i=1;i<=10;i++)
		// cout<<vis[6][i]<<endl;
	for(int i=n;i>=1;i--)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}
}
/*
(((())))()

*/


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
const int maxn=1e5+5;
int vis[maxn][15];
bool check(int idx)
{
	int num=-1;
	for(int i=1;i<=10;i++)
		if(vis[idx][i]==0)
			continue;
		else
		{
			if(num==-1)
				num=vis[idx][i];
			else if(num!=vis[idx][i])
				return 0;
		}
	return 1;

}
int main()
{
	int n;
	scanf("%d",&n);
	int a;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a);
		vis[i][a]=vis[i-1][a]+1;
	}
	for(int i=n;i>=1;i--)
	{
		if(check(i))
		{
			cout<<i<<endl;
			break;
		}
	}
}
/*
(((())))()

*/


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <ll>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
ll i , j, n, k, t,a, p, ct = 0 , ct1 = 0, ct2 = 0, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<ll, ll> m;
    fn(i , n){
        cin >> a;
        m[a]++;
        map<ll, ll>m1;
        ct = 0; ct1 = 0;ct2 = 0;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(m1[q] == 1 && q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(p == 1 && q == 1) v.pb(i + 1);
        }

    }
    cout << v.back();

}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <int>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
int i , j, n, k, t,a, p, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<int , int> m;
    fn(i , n){
        cin >> a;m[a]++;
        map<int,int>m1;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        //cout << p << " " << q << endl;
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(m.size() == 1) v.pb(i + 1);
        }
    }
    cout << v.back();
}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <int>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
int i , j, n, k, t,a, p, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<int , int> m;
    fn(i , n){
        cin >> a;m[a]++;
        map<int,int>m1;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(q == p + 1 && m[q] == 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(m.size() == 1) v.pb(i + 1);
            else if(p == q && q == 1) v.pb(i + 1);
        }
    }
    cout << v.back();
}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <ll>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
ll i , j, n, k, t,a, p, ct = 0 , ct1 = 0, ct2 = 0, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<ll, ll> m;
    fn(i , n){
        cin >> a;
        m[a]++;
        map<ll, ll>m1;
        ct = 0; ct1 = 0;ct2 = 0;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(m1[q] == 1 && q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(p == 1 && q == 1) {v.pb(n);break;}
        }

    }
    cout << v.back();

}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <int>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
int i , j, n, k, t,a, p, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<int , int> m;
    fn(i , n){
        cin >> a;m[a]++;
        map<int,int>m1;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        //cout << p << " " << q << endl;
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(q == p) v.pb(i + 1);
        }
    }
    cout << v.back();
}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <ll>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
ll i , j, n, k, t,a, p, ct = 0 , ct1 = 0, ct2 = 0, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<ll, ll> m;
    fn(i , n){
        cin >> a;
        m[a]++;
        map<ll, ll>m1;
        ct = 0; ct1 = 0;ct2 = 0;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(m1[q] == 1 && q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(p == 1 && q == 1) v.pb(i + 1);
            if(m.size() == 1) v.pb(i + 1);
        }

    }
    cout << v.back();

}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <ll>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
ll i , j, n, k, t,a, p, ct = 0 , ct1 = 0, ct2 = 0, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<ll, ll> m;
    fn(i , n){
        cin >> a;
        map<ll, ll>m1;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(m1[q] == 1 && q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(p == q && p == 1) v.pb(i + 1);
            else if(m.size() == 1) v.pb(i + 1);
        }
        m[a]++;

    }
    cout << v.back();

}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <ll>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
ll i , j, n, k, t,a, p, ct = 0 , ct1 = 0, ct2 = 0, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<ll, ll> m;
    fn(i , n){
        cin >> a;
        m[a]++;
        map<ll, ll>m1;
        ct = 0; ct1 = 0;ct2 = 0;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(p == 1 && q == 1) v.pb(i + 1);
            if(m.size() == 1) v.pb(i + 1);
        }

    }
    cout << v.back();

}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <int>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
int i , j, n, k, t,a, p, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<int , int> m;
    fn(i , n){
        cin >> a;m[a]++;
        map<int,int>m1;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(m.size() == 1) v.pb(i + 1);
            else if(m.size() > 1 && p == q && q == 1) v.pb(i + 1);
        }
    }
    cout << v.back();
}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <ll>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
ll i , j, n, k, t,a, p, ct = 0 , ct1 = 0, ct2 = 0, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<ll, ll> m;
    fn(i , n){
        cin >> a;
        m[a]++;
        map<ll, ll>m1;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(m1[q] == 1 && q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(p == q && p == 1) v.pb(i + 1);
            else if(m.size() == 1) v.pb(i + 1);
        }

    }
    cout << v.back();

}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <ll>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
ll i , j, n, k, t,a, p, ct = 0 , ct1 = 0, ct2 = 0, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<ll, ll> m;
    fn(i , n){
        cin >> a;
        m[a]++;
        map<ll, ll>m1;
        for(auto t: m) m1[t.S]++;
        p = 10000000000; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(m1[q] == 1 && q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(p == q && p == 1) v.pb(i + 1);
            else if(m.size() == 1) v.pb(i + 1);
        }

    }
    cout << v.back();

}


#include <bits/stdc++.h>
using namespace std;
#define fn(i,n) for(i = 0; i < n; i++)
#define fg(i,a ,n) for(i = a; i < n; i++)
#define fp(i,a, n) for(i = a; i >= n; i-- )
#define ld double
#define ll long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define S second
#define F first
#define vi vector <ll>
#define INF 10000000000
#define all(x) x.rbegin(), x.rend()
#define vl vector< pair <ll , ll>>
typedef pair<ll, ll> pl;
ll i , j, n, k, t,a, p, ct = 0 , ct1 = 0, ct2 = 0, q;
int32_t main(){
    IOS;
    cin >> n;
    vi v;map<ll, ll> m;
    fn(i , n){
        cin >> a;
        m[a]++;
        map<ll, ll>m1;
        ct = 0; ct1 = 0;ct2 = 0;
        for(auto t: m) m1[t.S]++;
        p = 11; q = 0;
        for(auto t: m){
            p = min(t.S , p);
            q = max(t.S , q);
        }
        if(m1.size() == 2){
            if(p == 1 && m1[p] == 1) v.pb(i + 1);
            else if(m1[q] == 1 && q == p + 1) v.pb(i + 1);
        }else if(m1.size() == 1){
            if(p == q) v.pb(i + 1);
        }

    }
    cout << v.back();

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define pause; system("pause");
#define mp make_pair
const int N=1e5+10;
const double eps=1e-9;
const int inf=1e9;
int a[N];
int frq[12];
map<int,int>frqfrq;
main(){

    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    if(n==1){
        printf("1");
        return 0;
    }
    int ans=1;
    frq[a[1]]++;
    for(int i=2;i<=n;i++){
        frqfrq[frq[a[i]]]--;
        frqfrq[frq[a[i]]]=max(0,frqfrq[frq[a[i]]]);
        frq[a[i]]++;
        frqfrq[frq[a[i]]]++;
        set<int>S;
        int cnt=0,o=0;
        for(int j=1;j<11;j++){
            if(frq[j]){
                S.insert(frq[j]);
                ++cnt;
            }
            if(frq[j]==1)++o;
        }
        if((int)S.size()>2);
        else if((int)S.size()==2){
            int l=*S.begin(),r=*S.rbegin();
            if((r-l==1 && frqfrq[r]==1) || (l==1 && o==1))ans=max(ans,i);
        }
        else if((int)S.size()==1){
            if(cnt==n)ans=max(ans,i);
            else if(cnt>1)ans=max(ans,i-1);
            else if(cnt==1)ans=max(ans,i);
        }
    }
    printf("%d",ans);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define pause; system("pause");
#define mp make_pair
const int N=1e5+10;
const double eps=1e-9;
const int inf=1e9;
int a[N];
int frq[12];
map<int,int>frqfrq;
main(){

    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    if(n==1){
        printf("1");
        return 0;
    }
    int ans=1;
    frq[a[1]]++;
    frqfrq[frq[a[1]]]++;
    for(int i=2;i<=n;i++){
        frqfrq[frq[a[i]]]--;
        frqfrq[frq[a[i]]]=max(0,frqfrq[frq[a[i]]]);
        frq[a[i]]++;
        frqfrq[frq[a[i]]]++;
        set<int>S;
        int cnt=0,o=0;
        for(int j=1;j<11;j++){
            if(frq[j]){
                S.insert(frq[j]);
                ++cnt;
            }
            if(frq[j]==1)++o;
        }
        if((int)S.size()>2);
        else if((int)S.size()==2){
            int l=*S.begin(),r=*S.rbegin();
            if((r-l==1 && frqfrq[r]==1) || (l==1 && o==1))ans=max(ans,i);
        }
        else if((int)S.size()==1){
            if(cnt==n)ans=max(ans,i);
            else if(cnt>1)ans=max(ans,i-1);
            else if(cnt==1)ans=max(ans,i);
        }
    }
    printf("%d",ans);

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define pause; system("pause");
#define mp make_pair
const int N=1e5+10;
const double eps=1e-9;
const int inf=1e9;
int a[N];
int frq[12];
map<int,int>frqfrq;
main(){

    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    if(n==1){
        printf("1");
        return 0;
    }
    int ans=1;
    frq[a[1]]++;
    for(int i=2;i<=n;i++){
        frqfrq[frq[a[i]]]--;
        frqfrq[frq[a[i]]]=max(0,frqfrq[frq[a[i]]]);
        frq[a[i]]++;
        frqfrq[frq[a[i]]]++;
        set<int>S;
        int cnt=0,o=0;
        for(int j=1;j<11;j++){
            if(frq[j]){
                S.insert(frq[j]);
                ++cnt;
            }
            if(frq[j]==1)++o;
        }
        if((int)S.size()>2)break;
        else if((int)S.size()==2){
            int l=*S.begin(),r=*S.rbegin();
            if((r-l==1 && frqfrq[r]==1) || (l==1 && o==1))ans=max(ans,i);
        }
        else if((int)S.size()==1){
            if(cnt==n)ans=max(ans,i);
            else if(cnt>1)ans=max(ans,i-1);
        }
    }
    printf("%d",ans);

}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string>

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <cassert>

using namespace std;

int C[4];

int main()
{

   int n,total=0;
   cin>>n;
   for(int i = 1; i <= n; i++) {
      int a;
      cin>>a;
      C[a]++;
      total+=a;
   }

   int ret = 0;

   if(total<3 || total == 5) {
      ret = -1;
   }else {
      if(C[1]+C[2]==0) ret = 0;
      else {
         if(C[1]==0) {
            C[3] += (C[2]/3)*2;
            ret += (C[2]/3)*2;
            C[2]%=3;
            if(C[2]==1) {
               if(C[4]) ret++;
               else ret+=2;
            }else ret += C[2];
         }else {
            int m = min(C[1],C[2]);
            ret += m;
            C[1]-=m;
            C[2]-=m;
            C[3]+=m;
            //now we have either 1 remaining or 2 remainings
            if(C[1]) {
               C[3]+=C[1]/3;
               ret += (C[1]/3)*2;
               C[1]%=3;
               ret += C[1];
            }else if(C[2]) {
               C[3] += (C[2]/3)*2;
               ret += (C[2]/3)*2;
               C[2]%=3;
               if(C[2]==1) {
                  if(C[4]) ret++;
                  else ret+=2;
               }else ret += C[2];
            }
         }
      }
   }
   cout<<ret<<endl;

   return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <string>

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <cassert>

using namespace std;

int C[4];

int main()
{

   int n,total=0;
   cin>>n;
   for(int i = 1; i <= n; i++) {
      int a;
      cin>>a;
      C[a]++;
      total+=a;
   }

   int ret = 0;

   if(total>3 || total == 5) {
      ret = -1;
   }else {
      if(C[1]+C[2]==0) ret = 0;
      else {
         if(C[1]==0) {
            C[3] += (C[2]/3)*2;
            ret += (C[2]/3)*2;
            C[2]%=3;
            if(C[2]==1) {
               if(C[4]) ret++;
               else ret+=2;
            }else ret += C[2];
         }else {
            int m = min(C[1],C[2]);
            ret += m;
            C[1]-=m;
            C[2]-=m;
            C[3]+=m;
            //now we have either 1 remaining or 2 remainings
            if(C[1]) {
               C[3]+=C[1]/3;
               ret += (C[1]/3)*2;
               C[1]%=3;
               ret += C[1];
            }else if(C[2]) {
               C[3] += (C[2]/3)*2;
               ret += (C[2]/3)*2;
               C[2]%=3;
               if(C[2]==1) {
                  if(C[4]) ret++;
                  else ret+=2;
               }else ret += C[2];
            }
         }
      }
   }
   cout<<ret<<endl;

   return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define PR(x) cout << #x " = " << x << "\n";
int n;
int cnt1=0, cnt2=0, cnt3=0, cnt4=0, ans=0;
void merge1with2(std::vector<int> &in)
{

    std::vector<int> ::iterator it1=in.begin();
    std::vector<int> ::iterator it2=lower_bound(in.begin(), in.end(), 3); it2--;
    while(it1!=in.end() && it2!=in.end() && *it1==1 && *it2==2)
    {
        *it1=0; cnt1--;
        *it2=3; cnt2--; cnt3++;
        ans++;
        it1++; it2--;
    }
    it1=upper_bound(in.begin(), in.end(), 0);
    if(it1==in.begin()) in.erase(in.begin());
    else in.erase(in.begin(), it1);
    /*for (int i = 0; i < in.size(); ++i)
    {
        cout<<in[i]<<" ";
    }
    cout<<endl;*/

}
void merge1with3(std::vector<int> &in)
{
    /*for (int i = 0; i < in.size(); ++i)
    {
        cout<<in[i]<<" ";
    }
    cout<<endl;*/
    std::vector<int> ::iterator it1=in.begin();
    std::vector<int> ::iterator it3=lower_bound(in.begin(), in.end(), 4); it3--;
    while(it1!=in.end() && it3!=in.end() && *it1==1 && *it3==3)
    {
        *it1=0; cnt1--;
        *it3=4; cnt3--; cnt4++;
        ans++;
        it1++; it3--;
    }
    it1=upper_bound(in.begin(), in.end(), 0);
    if(it1==in.begin()) in.erase(in.begin());
    in.erase(in.begin(), it1);
    /*for (int i = 0; i < in.size(); ++i)
    {
        cout<<in[i]<<" ";
    }
    cout<<endl;
    PR(ans);*/

}
void from4to2(std::vector<int> &in)
{
    std::vector<int> :: iterator it2=lower_bound(in.begin(), in.end(), 3); it2--;
    std::vector<int> :: iterator it4=lower_bound(in.begin(), in.end(), 4);
    while(it2!=in.end() && it4!=in.end() && *it2==2 && *it4==4)
    {
        *it4=3; cnt4--; cnt3++;
        *it2=3; cnt2--; cnt3++;
        ans++;
        it2--; it4++;
    }

}
int main(int argc, char const *argv[])
{
    ios_base::sync_with_stdio(0);
    int n, temp; cin>>n;
    std::vector<int> in;
    for (int i = 0; i < n; ++i)
    {
        cin>>temp;
        in.push_back(temp);
        if(in[i]==1) cnt1++;
        if(in[i]==2) cnt2++;
        if(in[i]==3) cnt3++;
        if(in[i]==4) cnt4++;
    }
    sort(in.begin(), in.end());
    if(cnt1!=0 && cnt2!=0 ) merge1with2(in);
    if (cnt1==0 && cnt2==0) { cout<<ans; return 0; }
    else if (cnt1==0) //2 3 4 left
    {
        from4to2(in);
        if(cnt2==0) { cout<<ans; return 0; }
        else
        {
            if(cnt2%3!=0) { cout<<-1; return 0; }
            else {cout<<ans + cnt2/3; return 0; }
        }
    }
    else // 1 3 4 left
    {
        merge1with3(in);
        if(cnt1==0) { cout<<ans; return 0; }
        else
        {
            ans+=cnt1/3;
            if(cnt1==0) { cout<<ans; return 0; }
            else
            {
                while(cnt1!=0 || cnt4>0)
                {
                    cnt1--; cnt4-=2; ans +=2;
                }
                if(cnt1==0) { cout<<ans; return 0; }
                else { cout<<-1; return 0; }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define PR(x) cout << #x " = " << x << "\n";
int n;
int cnt1=0, cnt2=0, cnt3=0, cnt4=0, ans=0;
void merge1with2(std::vector<int> &in)
{
    std::vector<int> ::iterator it1=in.begin();
    std::vector<int> ::iterator it2=upper_bound(in.begin(), in.end(), 3); it2--;
    while(it1!=in.end() && it2!=in.end() && *it1==1 && *it2==2)
    {
        *it1=0; cnt1--;
        *it2=3; cnt2--; cnt3++;
        ans++;
        it1++; it2--;
    }
    it1=upper_bound(in.begin(), in.end(), 0);
    if(it1==in.begin()) in.erase(in.begin());
    else in.erase(in.begin(), it1);
    /*for (int i = 0; i < in.size(); ++i)
    {
        cout<<in[i]<<" ";
    }
    cout<<endl;*/

}
void merge1with3(std::vector<int> &in)
{
    std::vector<int> ::iterator it1=in.begin();
    std::vector<int> ::iterator it3=upper_bound(in.begin(), in.end(), 4); it3--;
    while(it1!=in.end() && it3!=in.end() && *it1==1 && *it3==3)
    {
        *it1=0; cnt1--;
        *it3=4; cnt3--; cnt4++;
        ans++;
        it1++; it3--;
    }
    it1=upper_bound(in.begin(), in.end(), 0);
    in.erase(in.begin(), it1);

}
void from4to2(std::vector<int> &in)
{
    std::vector<int> :: iterator it2=upper_bound(in.begin(), in.end(), 3); it2--;
    std::vector<int> :: iterator it4=lower_bound(in.begin(), in.end(), 4);
    while(it2!=in.end() && it4!=in.end() && *it2==2 && *it4==4)
    {
        *it4=3; cnt4--; cnt3++;
        *it2=3; cnt2--; cnt3++;
        ans++;
        it2--; it4++;
    }
}
int main(int argc, char const *argv[])
{
    ios_base::sync_with_stdio(0);
    int n, temp; cin>>n;
    std::vector<int> in;
    for (int i = 0; i < n; ++i)
    {
        cin>>temp;
        in.push_back(temp);
        if(in[i]==1) cnt1++;
        if(in[i]==2) cnt2++;
        if(in[i]==3) cnt3++;
        if(in[i]==4) cnt4++;
    }
    sort(in.begin(), in.end());
    if(cnt1!=0 && cnt2!=0 ) merge1with2(in);
    if (cnt1==0 && cnt2==0) { cout<<ans; return 0; }
    else if (cnt1==0) //2 3 4 left
    {
        from4to2(in);
        if(cnt2==0) { cout<<ans; return 0; }
        else
        {
            if(cnt2%3!=0) { cout<<-1; return 0; }
            else {cout<<ans + cnt2/3; return 0; }
        }
    }
    else // 1 3 4 left
    {
        merge1with3(in);
        if(cnt1==0) { cout<<ans; return 0; }
        else
        {
            if(cnt1=cnt4*2) { cout<<ans+cnt1; return 0; }
            else { cout<<-1; return 0; }
        }
    }
    return 0;
}