#include <bits/stdc++.h>
using namespace std;
map<int, int> m;
set<pair<int, int> > s;
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int an = 1;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (m[a] == 0) {
      m[a] = 1;
    } else {
      s.erase(pair<int, int>(m[a], a));
      m[a]++;
    }
    s.insert(pair<int, int>(m[a], a));
    if (s.size() == 1)
      an = i + 1;
    else {
      int t = (++s.rbegin())->first;
      if (s.begin()->first == t && s.rbegin()->first == t + 1) an = i + 1;
      t = (++s.begin())->first;
      if (s.begin()->first == 1 && s.rbegin()->first == t) an = i + 1;
    }
  }
  cout << an << endl;
}
