#include <bits/stdc++.h>
using namespace std;
long long n, a[200000], kol[200000], t, p[200000], ans;
long long mp;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    t = p[a[i]];
    kol[t]--;
    p[a[i]]++;
    t++;
    kol[t]++;
    kol[0] = 0;
    mp = max(mp, p[a[i]]);
    if (kol[mp] == i) {
      ans = i;
      continue;
    }
    if (mp == 1) {
      ans = i;
      continue;
    }
    if (kol[mp] * mp == i - 1 && kol[1] == 1) {
      ans = i;
      continue;
    }
    if (kol[mp - 1] * (mp - 1) == i - mp && kol[mp] == 1) {
      ans = i;
      continue;
    }
  }
  cout << ans;
}
