#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, i;
  cin >> n;
  int colors[n];
  map<int, int> dem;
  map<int, int>::iterator im;
  for (i = 0; i < n; i++) {
    cin >> colors[i];
    dem[colors[i]]++;
  }
  i = n - 1;
  while (true) {
    vector<int> ss;
    for (im = dem.begin(); im != dem.end(); im++)
      if (im->second != 0) ss.push_back(im->second);
    sort(ss.begin(), ss.end());
    if (ss.size() == 1 || (ss.size() == 2 && ss[0] == 1) ||
        (ss[0] == 1 && ss.back() == ss[1]) ||
        (ss.back() - ss.front() == 1 && ss.back() != ss[ss.size() - 2])) {
      cout << i + 1;
      return 0;
    }
    dem[colors[i]]--;
    i--;
  }
  return 0;
}
