#include <bits/stdc++.h>
using namespace std;
map<int, int> cnt, ts;
int u[100005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &u[i]);
    cnt[u[i]]++;
  }
  for (map<int, int>::iterator p = cnt.begin(); p != cnt.end(); p++)
    ts[p->second]++;
  int ans = 1;
  map<int, int>::iterator p1, p2;
  for (int i = n; i >= 1; i--) {
    if (ts.size() == 1 && (ts.begin()->first == 1 || ts.begin()->second == 1)) {
      ans = i;
      break;
    } else if (ts.size() == 2) {
      p2 = ts.begin();
      p1 = p2++;
      if ((p1->first) == (p2->first) - 1 && p2->second == 1) {
        ans = i;
        break;
      } else if ((p1->first) == 1 && (p1->second) == 1) {
        ans = i;
        break;
      }
    }
    ts[cnt[u[i]]]--;
    if (ts[cnt[u[i]]] == 0) ts.erase(cnt[u[i]]);
    cnt[u[i]]--;
    ts[cnt[u[i]]]++;
    ts.erase(0);
  }
  printf("%d\n", ans);
  return 0;
}
