#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> freq, cnt;
int mx = 0;
bool check(int i) {
  if (cnt[1] == i) return true;
  if (cnt[i] == 1) return true;
  if (cnt[1] == 1 and cnt[mx] * mx == i - 1) return true;
  if (cnt[mx - 1] * (mx - 1) == i - mx and cnt[mx] == 1) return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  int n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int u;
    cin >> u;
    cnt[freq[u]]--;
    freq[u]++;
    cnt[freq[u]]++;
    mx = max(mx, freq[u]);
    if (check(i)) ans = i;
  }
  cout << ans << '\n';
}
