#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, j, x;
  cin >> n;
  map<long long, long long> mp;
  vector<long long> v;
  for (i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
    mp[x]++;
  }
  map<long long, long long, std::greater<long long> > occ;
  for (map<long long, long long>::iterator it = mp.begin(); it != mp.end();
       it++) {
    occ[it->second]++;
  }
  for (i = n - 1; i >= 0; i--) {
    if (occ[i + 1] == 1)
      return cout << i + 1, 0;
    else if (occ[1] == i + 1)
      return cout << i + 1, 0;
    else {
      while (occ.begin()->second == 0) occ.erase(occ.begin());
      map<long long, long long, greater<long long> >::iterator it2 =
                                                                   occ.begin(),
                                                               it = occ.begin();
      it2++;
      if (occ[1] == 1 && (it->second) * (it->first) == i)
        return cout << i + 1, 0;
      else if ((it->second) == 1 && (it->first) - 1 == it2->first &&
               i == (it2->second + 1) * (it2->first))
        return cout << i + 1, 0;
      else {
        occ[mp[v[i]]]--;
        mp[v[i]]--;
        occ[mp[v[i]]]++;
      }
    }
  }
  cout << 1;
  return 0;
}
