#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n], f[100001] = {0}, ff[100001] = {0}, maxf = -1, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    f[a[i]]++;
    if (f[a[i]] > maxf) maxf = f[a[i]];
    ff[f[a[i]]]++;
    ff[f[a[i]] - 1]--;
    if (ff[maxf] * maxf + 1 == i + 1) ans = i + 1;
    if (maxf != 1 && ff[maxf] == 1 && ff[maxf - 1] * (maxf - 1) + maxf == i + 1)
      ans = i + 1;
    else if (maxf == 1) {
      if (ff[maxf] == i + 1) ans = i + 1;
    }
  }
  cout << ans << endl;
}
