#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a, b, u, z, d[N], Cnt[N], ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    b = a;
    scanf("%d", &a);
    --Cnt[d[a]];
    ++Cnt[++d[a]];
    u = d[b];
    z = d[a];
    if (Cnt[z] == 1 && Cnt[z - 1] * (z - 1) + z == i) ans = i;
    if (z == 1 && (Cnt[u] - (u == 1)) * u == i - 1) ans = i;
    if (Cnt[z] * z + 1 == i) ans = i;
    if (Cnt[z] * z + z + 1 == i && Cnt[z + 1] == 1) ans = i;
  }
  cout << ans;
}
