#include <bits/stdc++.h>
const int N = 1e5 + 10;
int main() {
  int n, color, ans, mx = 0, f[100009] = {}, cnt[100009] = {}, q;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &color);
    cnt[f[color]]--;
    f[color]++;
    cnt[f[color]]++;
    if (f[color] > mx) mx = f[color];
    q = 0;
    if (cnt[1] == i)
      q = 1;
    else if (cnt[i] == 1)
      q = 1;
    else if (cnt[1] == 1 && cnt[mx] * mx == i - 1)
      q = 1;
    else if (cnt[mx - 1] * (mx - 1) == i - mx && cnt[mx] == 1)
      q = 1;
    if (q) ans = i;
  }
  printf("%d", ans);
  return 0;
}
