#include <bits/stdc++.h>
const int N = 100005;
const long double pi = 3.141592653;
const long long MOD = 1000000007;
using namespace std;
int dx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0};
int dy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, i;
  cin >> n;
  map<int, int> m;
  vector<int> u(n);
  for (i = 0; i < n; i++) {
    cin >> u[i];
    if (m.find(u[i]) != m.end())
      m[u[i]]++;
    else
      m.insert({u[i], 1});
  }
  for (i = n - 1; i >= 0; i--) {
    int ma = 0, mi = INT_MAX, cma = 0, cmi = 0, ver = -1;
    for (auto &o : m) {
      ma = max(ma, o.second);
      mi = min(mi, o.second);
    }
    if (ma == mi && m.size() == 1)
      ver = 1;
    else if (ma == mi && m.size() > 1 && mi == 1)
      ver = 1;
    else if (ma == mi && m.size() > 1 && mi != 1)
      ver = 0;
    else {
      for (auto &o : m) {
        if (o.second == ma)
          cma++;
        else if (o.second == mi)
          cmi++;
      }
      if (cma + cmi != m.size())
        ver = 0;
      else {
        if ((ma - mi == 1 && cma == 1) || (mi == 1 && cmi == 1))
          ver = 1;
        else
          ver = 0;
      }
    }
    if (ver == 0) {
      m[u[i]]--;
      if (m[u[i]] == 0) m.erase(u[i]);
    } else
      break;
  }
  cout << i + 1;
  return 0;
}
