#include <bits/stdc++.h>
using namespace std;
long long n, i, j, l, m, k, a[301010], b, c[301010], d[20];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> k;
    if (c[k]) a[c[k]]--;
    c[k]++;
    a[c[k]]++;
    l = max(l, c[k]);
    if (a[1] == i) {
      m = i;
    }
    if (a[1] == 1 && l * (a[l]) == i - 1) {
      m = i;
    }
    if (a[l] == 1 && (l - 1) * (a[l - 1]) == i - l) {
      m = i;
    }
  }
  cout << m;
}
