#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vec<int> A(N);
  for (int i = 0; i < N; i++) cin >> A[i];
  auto add = [](map<int, int>& m, int a) { m[a]++; };
  auto del = [](map<int, int>& m, int a) {
    m[a]--;
    if (m[a] <= 0) m.erase(a);
  };
  auto check = [](map<int, int>& m) {
    if (m.size() > 2) return false;
    if (m.size() == 1) {
      return (m.begin()->first) == 1 || (m.begin()->second) == 1;
    }
    vec<pair<int, int>> v;
    for (auto& x : m) v.push_back(x);
    if (v[0].first == 1 && v[0].second == 1) return true;
    if (v[0].first + 1 != v[1].first) return false;
    return v[1].second == 1;
  };
  map<int, int> cnt, num;
  int ans = 1;
  for (int i = 0; i < N; i++) {
    del(num, cnt[A[i]]);
    cnt[A[i]]++;
    add(num, cnt[A[i]]);
    if (check(num)) ans = i + 1;
  }
  cout << ans << endl;
}
