#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
typedef long long ll;
#define cerr cout
#define sz(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
inline int add(int a, int b){a += b; if(a >= MOD)a -= MOD; return a;}
inline int sub(int a, int b){a -= b; if(a < 0)a += MOD; return a;}
inline int mul(int a, int b){return (int)((long long) a * b %MOD);}
inline int binpow(int a, int b){int res = 1; while(b > 0){ if(b & 1)res = mul(res, a); a = mul(a, a);b /= 2;} return res;}
inline int inv(int a){return binpow(a, MOD - 2);}
int gcd(int a, int b, int &x, int &y){if(a == 0){x = 0, y = 1; return b;} int x1, y1; int d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}
const int N = 1e5 + 5;
int a[N], cnt[N];
int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
int n; cin>>n;
for(int i=0;i<n;++i){
cin>>a[i]; cnt[a[i]]++;
}
set<pair<int, int> > s;
for(int i=1;i<N;++i)if(cnt[i])s.insert({cnt[i], i});
for(int i=n-1;i>=0;--i){
if(s.size() == 1){
cout<<i + 1<<"\n"; return 0;
}
int val = a[i];
auto it = s.begin();
int first = (*it).first; it++;
int secfirst = (*it).first;
auto it1 = s.rbegin(); int last = (*it1).first;
if(i == 8)pr(s), pr(*it1);
it1--;
if(i == 8)pr(*it1);
int seclast = (*it1).first;
if((first == 1 && secfirst == last) || (last - 1 == first && seclast == first)){
cout<<i + 1<<"\n"; return 0;
}
s.erase({cnt[val], val}); cnt[val]--;
if(cnt[val] > 0)s.insert({cnt[val], val});
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKI2RlZmluZSBjZXJyIGNvdXQKI2RlZmluZSBzeihhKSAoaW50KSgoYSkuc2l6ZSgpKQojZGVmaW5lIGFsbCh4KSAoeCkuYmVnaW4oKSwgKHgpLmVuZCgpCnN0cmluZyB0b19zdHJpbmcoc3RyaW5nIHMpIHsgcmV0dXJuICciJyArIHMgKyAnIic7fQpzdHJpbmcgdG9fc3RyaW5nKGNoYXIgcykgeyByZXR1cm4gc3RyaW5nKDEsIHMpO30Kc3RyaW5nIHRvX3N0cmluZyhjb25zdCBjaGFyKiBzKSB7IHJldHVybiB0b19zdHJpbmcoKHN0cmluZykgcyk7fQpzdHJpbmcgdG9fc3RyaW5nKGJvb2wgYikgeyByZXR1cm4gKGIgPyAidHJ1ZSIgOiAiZmFsc2UiKTt9CnRlbXBsYXRlIDx0eXBlbmFtZSBBPiBzdHJpbmcgdG9fc3RyaW5nKEEpOwp0ZW1wbGF0ZSA8dHlwZW5hbWUgQSwgdHlwZW5hbWUgQj5zdHJpbmcgdG9fc3RyaW5nKHBhaXI8QSwgQj4gcCkge3JldHVybiAiKCIgKyB0b19zdHJpbmcocC5maXJzdCkgKyAiLCAiICsgdG9fc3RyaW5nKHAuc2Vjb25kKSArICIpIjt9CnRlbXBsYXRlIDx0eXBlbmFtZSBBPiBzdHJpbmcgdG9fc3RyaW5nKEEgdikge2Jvb2wgZiA9IDE7IHN0cmluZyByID0gInsiOyBmb3IgKGNvbnN0IGF1dG8gJnggOiB2KSB7aWYgK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