#include <bits/stdc++.h>
using namespace std;
const int64_t M = 1e5 + 5;
int64_t n, ans;
map<int64_t, int64_t> mp, dist;
int32_t main() {
  cin >> n;
  for (int64_t i = 1; i <= n; i++) {
    int64_t x;
    cin >> x;
    mp[x]++;
    dist[mp[x]]++;
    if (mp[x] != 1) {
      dist[mp[x] - 1]--;
      if (!dist[mp[x] - 1]) dist.erase(mp[x] - 1);
    }
    if (dist.size() == 1) {
      if (dist.begin()->first == 1 || dist.begin()->second == 1)
        ans = max(ans, i);
    } else if (dist.size() == 2) {
      auto it = dist.begin();
      auto cnt1 = *it;
      it++;
      auto cnt2 = *it;
      if (cnt1.second == 1 && cnt1.first <= 1) ans = max(ans, i);
      if (cnt2.second == 1 && cnt2.first - cnt1.first == 1) ans = max(ans, i);
    }
  }
  cout << ans;
  return 0;
}
