#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1, 0);
  for (int i = 1; i <= n; i++) cin >> a[i];
  vector<int> c(100001, 0), d(100002, 0);
  int res = 1;
  d[0] = n;
  int mdcai = 0, mcai = 0, mi;
  for (int i = 1; i <= n; i++) {
    d[c[a[i]]]--;
    c[a[i]]++;
    d[c[a[i]]]++;
    if ((d[c[a[i]]] == 1) && (d[c[a[i]] - 1] * (c[a[i]] - 1) + c[a[i]] == i))
      res = i;
    if ((d[c[a[i]] + 1] == 1) && (d[c[a[i]]] * (c[a[i]]) + c[a[i]] + 1 == i))
      res = i;
    if (d[0] + d[1] == n) res = min(n, d[1] + 1);
    if ((d[1] == 1) && ((d[c[a[i]]] * c[a[i]] == (i - 1) ||
                         (d[c[a[i]] - 1] * (c[a[i]] - 1) == (i - 1)))))
      res = i;
    if (d[c[a[i]]] >= mdcai) {
      mdcai = d[c[a[i]]];
      mcai = c[a[i]];
      mi = i;
    }
    if (mdcai * mcai == (mi - 1)) res = max(res, mi);
    if ((mdcai * mcai == mi) && (mi < n)) res = max(res, mi + 1);
  }
  cout << min(n, res);
  return 0;
}
