#include <bits/stdc++.h>
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  std::cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
std::pair<int, int> operator+(std::pair<int, int> a, std::pair<int, int> b) {
  return {a.first + b.first, a.second + b.second};
}
std::pair<long long, long long> operator+(std::pair<long long, long long> a,
                                          std::pair<long long, long long> b) {
  return {a.first + b.first, a.second + b.second};
}
std::ostream& operator<<(std::ostream& out, std::pair<int, int> a) {
  out << a.first << " " << a.second << "\n";
  return out;
}
std::ostream& operator<<(std::ostream& out, std::pair<long long, long long> a) {
  out << a.first << " " << a.second << "\n";
  return out;
}
std::istream& operator>>(std::istream& in, std::pair<int, int>& a) {
  in >> a.first >> a.second;
  return in;
}
std::istream& operator>>(std::istream& in, std::pair<long long, long long>& a) {
  in >> a.first >> a.second;
  return in;
}
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  int u[n];
  for (int i = 0; i < n; ++i) cin >> u[i];
  int cnt[100005] = {0};
  int val[100005] = {0};
  val[0] = n;
  int ans = 0;
  set<int> x;
  for (int i = 0; i < n; ++i) {
    cnt[u[i]]++;
    val[cnt[u[i]] - 1]--;
    if (val[cnt[u[i]] - 1] == 0) x.erase(cnt[u[i]] - 1);
    val[cnt[u[i]]]++;
    x.insert(cnt[u[i]]);
    if ((int)x.size() <= 2) {
      if ((int)x.size() == 1) {
        int a = *x.begin();
        if (a == 1) ans = i + 1;
        if (val[a] == 1) ans = i + 1;
      }
      if ((int)x.size() == 2) {
        int a = *x.begin();
        int b = *(++x.begin());
        if (a == 1) {
          if (val[1] == 1) ans = i + 1;
        }
        if (b - a == 1) {
          if (val[b] == 1) ans = i + 1;
        }
      }
    }
  }
  cout << ans;
}
