#include <bits/stdc++.h>
using namespace std;
const int mxn = 5e5 + 123;
const int inf = 1e9;
const int mod = 1e9 + 7;
long long n, x[mxn], first[mxn], mx, ok, l = 1;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int c;
    cin >> c;
    x[first[c]]--;
    first[c]++;
    x[first[c]]++;
    mx = max(mx, first[c]);
    ok = 0;
    if (x[1] == i || x[i] == 1) ok = 1;
    if (x[1] == 1 && x[mx] * mx == i - 1) ok = 1;
    if (x[mx] == 1 && x[mx - 1] * (mx - 1) == i - mx) ok = 1;
    if (ok == 1) l = i;
  }
  cout << l;
  return 0;
}
