#include <bits/stdc++.h>
using namespace std;
int n, u[100005], maxx = 1, f[100005], c[100005], res = 1, d[100005], dem;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> u[i];
  if (n == 1)
    cout << "1";
  else {
    for (int i = 1; i <= n; ++i) {
      if (!d[u[i]]) {
        d[u[i]] = 1;
        ++dem;
      }
      if (c[u[i]]) f[c[u[i]]]--;
      c[u[i]]++;
      f[c[u[i]]]++;
      maxx = max(maxx, c[u[i]]);
      if (maxx == 1) {
        res = max(res, i);
      }
      if (f[maxx] == 1 && f[maxx - 1] == dem - 1) {
        res = max(res, i);
      }
      if (f[1] == 1 && f[maxx] == dem - 1) {
        res = max(res, i);
      }
    }
    cout << res;
  }
}
