#include <bits/stdc++.h>
using ll = long long;
using namespace std;
bool is_valid_streak(map<ll, ll>& unique_counts) {
  if (unique_counts.size() == 2 && unique_counts.begin()->first == 1 &&
      unique_counts.begin()->second == 1) {
    return true;
  } else if (unique_counts.size() == 2 &&
             unique_counts.begin()->first + 1 ==
                 unique_counts.rbegin()->first &&
             unique_counts.rbegin()->second == 1) {
    return true;
  }
  return false;
}
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll, ll> counts;
  map<ll, ll> unique_counts;
  set<ll> unique_values;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (counts[a[i]] != 0) {
      unique_counts[counts[a[i]]]--;
      if (unique_counts[counts[a[i]]] == 0) {
        unique_counts.erase(counts[a[i]]);
      }
    }
    counts[a[i]]++;
    unique_counts[counts[a[i]]]++;
    unique_values.insert(a[i]);
    if (unique_values.size() == 1) {
      ans = i + 1;
    } else if (unique_values.size() == i + 1) {
      ans = i + 1;
    } else if (is_valid_streak(unique_counts)) {
      ans = i + 1;
    }
  }
  cout << ans << endl;
}
