#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005, inf = 1e9 + 5;
const long long INF = 1e18 + 5;
const long double eps = 1e-9;
multiset<int> ss;
int cnt[MAXN];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int ans = 1;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    int p = cnt[c];
    if (p != 0) ss.erase(ss.find(p));
    ss.insert(++cnt[c]);
    if (ss.size() > 1) {
      auto it = ss.begin();
      int v1 = *it, v2;
      it++;
      v2 = *it;
      auto itt = ss.rbegin();
      int l1 = *itt, l2;
      itt++;
      l2 = *itt;
      if (((l1 - v1) == 1 && v1 == l2) || (v1 == 1 && v2 == l1)) {
        ans = i;
      }
    } else
      ans = i;
  }
  cout << ans << endl;
  return 0;
}
