#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long MOD = 1e9 + 7;
const int MAXN = 200010;
map<int, int> occr;
multiset<int> cnt;
void Remove(int x) {
  multiset<int>::iterator it = cnt.find(x);
  if (it != cnt.end()) {
    cnt.erase(it);
  }
}
int main() {
  int n, m;
  occr.clear();
  cnt.clear();
  cin >> n;
  int u;
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &u);
    int lastOccr = 0;
    if (occr.find(u) != occr.end()) {
      lastOccr = occr[u];
    } else {
      lastOccr = 0;
    }
    Remove(lastOccr);
    lastOccr++;
    cnt.insert(lastOccr);
    occr[u] = lastOccr;
    if (cnt.size() == 1) {
      ans = i;
      continue;
    }
    if (*cnt.begin() == 1) {
      multiset<int>::iterator second = cnt.begin();
      second++;
      if (*second == *cnt.rbegin()) {
        ans = i;
      }
    }
    {
      multiset<int>::reverse_iterator rsecond = cnt.rbegin();
      rsecond++;
      if ((*cnt.begin()) + 1 == *cnt.rbegin() && *cnt.begin() == *rsecond) {
        ans = i;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
