#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("mmx,avx,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long ans = 0;
  map<long long, long long> cnt;
  map<long long, long long> dif;
  for (long long i = 0; i < n; i++) {
    if (dif[cnt[a[i]]]) dif[cnt[a[i]]]--;
    if (dif[cnt[a[i]]] == 0) dif.erase(cnt[a[i]]);
    cnt[a[i]]++;
    dif[cnt[a[i]]]++;
    if (dif.size() == 1 && (cnt.size() == 1 || (*(dif.begin())).first == 1))
      ans = i + 1;
    else {
      if (dif.size() == 2) {
        long long t1 = (*(dif.begin())).first;
        long long ch1 = (*(dif.begin())).second;
        long long t2 = (*(next(dif.begin()))).first;
        long long ch2 = (*(next(dif.begin()))).second;
        if (ch1 == 1 && (t1 == 1 || t1 - 1 == t2))
          ans = i + 1;
        else if (ch2 == 1 && (t2 == 1 || t2 - 1 == t1))
          ans = i + 1;
      }
    }
  }
  cout << ans << '\n';
}
