#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
int n;
int cnt[N];
multiset<int> q;
multiset<int>::iterator it;
int main() {
  mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n;
  int answ = 0;
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    ++cnt[a];
    if (cnt[a] > 1) q.erase(q.find(cnt[a] - 1));
    q.insert(cnt[a]);
    if ((int)q.size() == 1) {
      answ = i;
      continue;
    }
    if ((int)q.size() == 2) {
      int x = (*q.begin());
      it = q.end();
      --it;
      int y = *it;
      if (y == x + 1 || x == 1) {
        answ = i;
      }
      continue;
    }
    int x = (*q.begin());
    it = q.end();
    --it;
    int y = *it;
    --it;
    int z = *it;
    if (x == z && x + 1 == y) {
      answ = i;
    }
    x = (*q.begin());
    it = q.begin();
    ++it;
    y = *it;
    it = q.end();
    --it;
    z = *it;
    if (x == 1 && y == z) {
      answ = i;
    }
  }
  cout << answ << endl;
  return 0;
}
