#include <bits/stdc++.h>
using namespace std;
int n, maxx;
vector<int> v, cnt;
map<int, int> ma;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  maxx = min(2, n);
  v.resize(n + 1);
  cnt.resize(100001);
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    map<int, int>::iterator it;
    it = ma.find(cnt[v[i]]);
    if (it != ma.end()) {
      it->second -= 1;
      if (it->second == 0) ma.erase(cnt[v[i]]);
    }
    cnt[v[i]] += 1;
    it = ma.find(cnt[v[i]]);
    if (it == ma.end())
      ma.insert(pair<int, int>(cnt[v[i]], 1));
    else
      it->second += 1;
    int chk = 1;
    if (ma.size() == 1) {
      chk = 0;
      pair<int, int> ff;
      ff = *ma.begin();
      if (ff.first == 1)
        chk = 1;
      else if (ff.second == 1)
        chk = 1;
    }
    if (ma.size() > 2) chk = 0;
    if (ma.size() == 2) {
      chk = 0;
      pair<int, int> ff, ss;
      it = ma.begin();
      ff = *it;
      it++;
      ss = *it;
      if ((ff.first == 1 && ff.second == 1) ||
          (ss.first == ff.first + 1 && ss.second == 1))
        chk = 1;
    }
    if (chk == 1) maxx = max(maxx, i);
  }
  cout << maxx;
  return 0;
}
