#include <bits/stdc++.h>
using namespace std;
int n, a[200200], ans, f[200200], f1[200200];
set<pair<int, int> > s;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (f[a[i]]) {
      s.erase({f[a[i]], a[i]});
      f1[f[a[i]]]--;
    }
    f[a[i]]++;
    f1[f[a[i]]]++;
    s.insert({f[a[i]], a[i]});
    pair<int, int> x = *s.begin();
    pair<int, int> y = *(--s.end());
    if (s.size() == 1)
      ans = i;
    else if (x.first == y.first && x.first == 1)
      ans = i;
    else if (x.first * f1[x.first] == i - 1 || y.first * f1[y.first] == i - 1)
      ans = i;
    else if (x.first + 1 == y.first || x.first - 1 == y.first) {
      if (f1[max(x.first, y.first)] == 1 &&
          x.first * f1[x.first] + y.first * f1[y.first] == i)
        ans = i;
    }
  }
  cout << ans;
  return 0;
}
