#include <bits/stdc++.h>
using namespace std;
int a[100010];
int b[100010];
int main() {
  int n;
  cin >> n;
  int ans = 0;
  int lol = 0;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    a[tmp]++;
    b[a[tmp]]++;
    b[a[tmp] - 1]--;
    lol = max(lol, a[tmp]);
    if (b[1] == i + 1) {
      ans = i + 1;
      continue;
    }
    if (b[i + 1] == 1) {
      ans = i + 1;
      continue;
    }
    if (b[1] == 1 && b[lol] * lol == i) {
      ans = i + 1;
      continue;
    }
    if (b[lol] == 1 && b[lol - 1] * (lol - 1) == i - lol + 1) {
      ans = i + 1;
      continue;
    }
  }
  cout << ans;
}
