#include <bits/stdc++.h>
using namespace std;
int freq[100005];
map<int, int> distr;
int main() {
  int n;
  cin >> n;
  int best = 0;
  for (int i = 1; i <= n; i++) {
    int u;
    cin >> u;
    if (freq[u]) {
      auto it = distr.find(freq[u]);
      if (!(--it->second)) {
        distr.erase(it);
      }
    }
    distr[++freq[u]]++;
    if ((distr.size() == 1 &&
         (distr.begin()->first == 1 || distr.begin()->second == 1)) ||
        (distr.size() == 2 &&
         ((distr.rbegin()->first - distr.begin()->first == 1 &&
           distr.rbegin()->second == 1) ||
          (distr.begin()->first == 1 && distr.begin()->second == 1)))) {
      best = i;
    }
  }
  cout << best;
}
