#include <bits/stdc++.h>
using namespace std;
int n, a[100009], cre[100009], prec[100009];
set<int> s, setr_2;
multiset<int> set_1;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) set_1.insert(0);
  if (n <= 2) {
    cout << n << endl;
    return 0;
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (prec[cre[a[i]]] == 1) setr_2.erase(cre[a[i]]);
    s.insert(a[i]);
    prec[cre[a[i]]]--;
    set_1.erase(set_1.lower_bound(cre[a[i]]));
    cre[a[i]]++;
    prec[cre[a[i]]]++;
    setr_2.insert(cre[a[i]]);
    set_1.insert(cre[a[i]]);
    if (s.size() == 1) res = max(res, i);
    if (setr_2.size() == 1 && (*setr_2.begin() == 1)) res = max(res, i);
    if (setr_2.size() == 2) {
      int x = *setr_2.begin();
      if (x == 1 && prec[x] == 1) res = max(res, i);
      int y = (*(++setr_2.begin()));
      if (y - x == 1 && prec[y] == 1) {
        res = max(res, i);
      }
    }
  }
  cout << res + 1 << endl;
}
