#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int arr[N], grr[100006] = {0}, fre[100005] = {0}, x, ma = 0, ans = 0;
  for (int i = 0; i < N; i++) {
    bool ok = false;
    cin >> x;
    grr[fre[x]]--;
    fre[x]++;
    grr[fre[x]]++;
    ma = max(ma, fre[x]);
    if (grr[1] == i + 1)
      ok = true;
    else if (grr[i + 1] == 1)
      ok = true;
    else if (grr[ma - 1] * (ma - 1) == i + 1 - ma && grr[ma] == 1)
      ok = true;
    else if (grr[1] == 1 && grr[ma] * (ma) == i)
      ok = true;
    if (ok) ans = i + 1;
  }
  cout << ans;
}
