#include <bits/stdc++.h>
using namespace std;
long long read(long long x = 0, long long fr = 1, char c = getchar()) {
  while (c < '0' || c > '9') fr *= (c == '-') ? -1 : 1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * fr;
}
int n, a[100010], app[100010], w[100010], colcnt, mx = 0;
int ans = 0;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) {
    app[a[i]]++;
    if (app[a[i]] != 1) w[app[a[i]] - 1]--;
    w[app[a[i]]]++;
    if (app[a[i]] == 1) colcnt++;
    mx = max(mx, app[a[i]]);
    if (w[1] == 1 && w[mx] == colcnt - 1) ans = i;
    if (w[mx] == 1 && w[mx - 1] == colcnt - 1) ans = i;
    if (mx == 1) ans = i;
  }
  printf("%d", ans);
  return 0;
}
