#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int cnt[N + 1];
int main() {
  set<pair<int, int> > s;
  int n, x, MAX = 0, cntMax = 0, MIN = 1e9, cntMin = 0, res;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x;
    set<pair<int, int> >::iterator itr = s.find({cnt[x], x});
    if (itr != s.end()) {
      s.erase(itr);
      if (cnt[x] == MIN) --cntMin;
      if (cnt[x] == MAX) --cntMax;
    }
    ++cnt[x];
    s.insert({cnt[x], x});
    itr = s.begin();
    pair<int, int> tmp = *itr;
    if (MIN != tmp.first) cntMin = 0;
    MIN = tmp.first;
    itr = s.end();
    --itr;
    tmp = *itr;
    if (MAX != tmp.first) cntMax = 0;
    MAX = tmp.first;
    if (cnt[x] == MIN) ++cntMin;
    if (cnt[x] == MAX) ++cntMax;
    if (s.size() > 1) {
      itr = s.begin();
      ++itr;
      tmp = *itr;
    }
    if ((MAX == MIN + 1 && cntMax == 1) ||
        (MIN == 1 && cntMin == 1 && s.size() > 1 && tmp.first == MAX) ||
        (MAX == MIN && MAX == 1) || (MAX == MIN && s.size() == 1))
      res = i;
  }
  cout << res;
}
