#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int MX = 3e5 + 9;
int n, a[MX], c[MX], p[MX];
set<int> s, s2;
multiset<int> s1;
int mx = 0, ans = 0;
int main() {
  cin >> n;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (n == 2) {
    cout << 2 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) s1.insert(0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    s.insert(a[i]);
    if (p[c[a[i]]] == 1) s2.erase(c[a[i]]);
    p[c[a[i]]]--;
    s1.erase(s1.lower_bound(c[a[i]]));
    c[a[i]]++;
    p[c[a[i]]]++;
    s1.insert(c[a[i]]);
    s2.insert(c[a[i]]);
    if (s.size() == 1) {
      ans = max(ans, i + 1);
    }
    if (s2.size() > 2) continue;
    if (s2.size() == 1) {
      if (*s2.begin() == 1) {
        ans = max(ans, i + 1);
      }
    } else {
      int x = *s2.begin();
      int y = (*(++s2.begin()));
      if (y - x == 1 && p[y] == 1) {
        ans = max(ans, i + 1);
      }
      if (x == 1 && p[x] == 1) ans = max(ans, i + 1);
    }
  }
  cout << ans << endl;
}
