#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long a[n];
  map<long long, long long> b, d;
  long long cnt = 0, c = 0, ans = 1, temp = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
    d[b[a[i]]]++;
    cnt++;
    if (b[a[i]] * d[b[a[i]]] == cnt && cnt + 1 <= n)
      ans = max(ans, cnt + 1);
    else if (b[a[i]] * d[b[a[i]]] == cnt - 1)
      ans = max(ans, cnt);
  }
  cout << ans << endl;
}
