#include <bits/stdc++.h>
using namespace std;
template <typename T>
void dbg(T t) {
  cerr << t << endl;
}
template <typename T, typename... Args>
void dbg(T t, Args... args) {
  cerr << t << " ";
  dbg(args...);
}
int occ[100001];
int a;
map<int, int> s;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  int n, ans = 0;
  map<int, int> m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    occ[a]++;
    if (occ[a] == 1) {
      m[1]++;
    } else if (occ[a] > 1) {
      m[occ[a] - 1]--;
      if (m[occ[a] - 1] == 0) {
        m.erase(occ[a] - 1);
      }
      m[occ[a]]++;
    }
    if (m.size() == 1) {
      if (m.begin()->first == 1) ans = i + 1;
      if (m.begin()->second == 1) ans = i + 1;
    }
    if (m.size() == 2) {
      auto it = m.begin();
      auto it2 = next(it);
      if (it->first == it2->first - 1 && it2->second == 1 ||
          (it->first == 1 && it->second == 1)) {
        ans = i + 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
