#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int main() {
  int N;
  cin >> N;
  vector<int> U(N);
  for (auto &u : U) cin >> u;
  map<int, int> counter, numbers;
  int ans = 0, maxnum = 0;
  for (int i = 0; i < N; i++) {
    int k = ++counter[U[i]];
    maxnum = max(maxnum, k);
    numbers[k - 1]--;
    numbers[k]++;
    if (numbers[1] == 1 && numbers[maxnum] * maxnum == i) {
      ans = i + 1;
    } else if (k == 1 && numbers[1] == i + 1) {
      ans = i + 1;
    } else {
      int a = numbers[k - 1];
      int b = numbers[k];
      int c = numbers[k + 1];
      if (c == 1 && b * k == (i + 1) - (k + 1)) ans = i + 1;
      if (b == 1 && a * (k - 1) == (i + 1) - k) ans = i + 1;
    }
  }
  cout << ans << '\n';
  return 0;
}
