#include <bits/stdc++.h>
using namespace std;
bool check(map<long long, long long> m) {
  if (m.size() <= 1) return 1;
  vector<long long> a;
  for (auto x : m) a.push_back(x.second);
  sort(a.begin(), a.end());
  a[0]--;
  long long flag = 1;
  long long val = a[0];
  if (a[0] == 0) val = a[1];
  for (long long i = 1; i < (long long)a.size(); i++) {
    if (a[i] != val) flag = 0;
  }
  if (flag) return 1;
  a[0]++;
  flag = 1;
  a.back()--;
  val = a.back();
  if (val == 0) val = a[0];
  for (long long i = 0; i < (long long)a.size() - 1; i++) {
    if (a[i] != val) flag = 0;
  }
  if (flag) return 1;
  return 0;
}
int32_t main() {
  long long n;
  cin >> n;
  map<long long, long long> m;
  long long a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
  }
  for (long long i = n - 1; i >= 0; i--) {
    if (check(m)) {
      cout << i + 1 << "\n";
      return 0;
    }
    m[a[i]]--;
    if (m[a[i]] == 0) m.erase(a[i]);
  }
  return 0;
}
