#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int MAXN = 200015;
int n;
int values[MAXN], cnt[MAXN];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> values[i];
    values[i]--;
  }
  int answer = 1;
  multiset<int> counts;
  for (int i = 0; i < n; i++) {
    if (counts.find(cnt[values[i]]) != counts.end()) {
      counts.erase(counts.find(cnt[values[i]]));
    }
    cnt[values[i]]++;
    counts.insert(cnt[values[i]]);
    if (counts.size() == 1) {
      answer = i + 1;
    }
    int lowestValue = *counts.begin();
    int highestValue = *(prev(counts.end()));
    int secondHighestValue = *(prev(prev(counts.end())));
    if (lowestValue == secondHighestValue &&
        secondHighestValue + 1 == highestValue) {
      answer = i + 1;
    }
    int secondLowestValue = *(next(counts.begin()));
    if (secondLowestValue == highestValue && lowestValue == 1) {
      answer = i + 1;
    }
  }
  cout << answer << endl;
  return 0;
}
