#include <bits/stdc++.h>
using namespace std;
signed main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int &i : v) {
    cin >> i;
  }
  map<int, int> m;
  map<int, int> p;
  int mi, ma;
  int sol = 1;
  int nmin = 1;
  m[v[0]]++;
  p[m[v[0]]]++;
  for (int i = 1; i < n; i++) {
    if (m[v[i]] > 0) {
      p[m[v[i]]]--;
      if (p[m[v[i]]] == 0) {
        p.erase(m[v[i]]);
      }
    }
    m[v[i]]++;
    p[m[v[i]]]++;
    mi = (*p.begin()).first;
    ma = (*p.rbegin()).first;
    if (p.size() == 2 && (*p.rbegin()).second == 1 &&
        (*p.rbegin()).first == 1 + (*p.begin()).first) {
      sol = i + 1;
    }
    if (p.size() == 2 && (*p.begin()).first == 1 && (*p.begin()).second == 1) {
      sol = i + 1;
    }
    if (p.size() == 1 &&
        ((*p.begin()).second == 1 || (*p.begin()).first == 1)) {
      sol = i + 1;
    }
  }
  cout << sol;
}
