#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 100005;
struct node {
  int count, num;
  node(int count, int num) : count(count), num(num) {}
  bool operator<(const node& o) const {
    return count < o.count || count == o.count && num < o.num;
  }
};
int n, A[MAXN], C[MAXN];
int main() {
  cin >> n;
  set<node> counts;
  int best = 0;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    auto it = counts.find(node(C[A[i]], A[i]));
    if (it != counts.end()) {
      counts.erase(it);
    }
    counts.insert(node(++C[A[i]], A[i]));
    node first = *counts.begin();
    node second = *next(counts.begin());
    node last = *counts.rbegin();
    node pen = *next(counts.rbegin());
    bool works = false;
    if (counts.size() == 1) {
      works = true;
    } else if (first.count == 1 && second.count == last.count) {
      works = true;
    } else if ((last.count == 1 || last.count - 1 == pen.count) &&
               pen.count == first.count) {
      works = true;
    }
    if (works) {
      best = i + 1;
    }
  }
  cout << best << endl;
  return 0;
}
