#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e5 + 5;
int main() {
  int n, f[MaxN], cnt[MaxN], mm = 0, ans = 0;
  scanf("%d", &n);
  memset(f, 0, sizeof(f));
  memset(cnt, 0, sizeof(cnt));
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    cnt[f[a]]--;
    f[a]++;
    cnt[f[a]]++;
    mm = max(mm, f[a]);
    bool flag = 0;
    if (cnt[1] == i)
      flag = 1;
    else if (cnt[i] == 1)
      flag = 1;
    else if (cnt[1] == 1 && cnt[mm] * mm == i - 1)
      flag = 1;
    else if (cnt[mm - 1] * (mm - 1) == i - mm && cnt[mm] == 1)
      flag = 1;
    if (flag == 1) ans = i;
  }
  printf("%d\n", ans);
  return 0;
}
