#include <bits/stdc++.h>
using namespace std;
int n, cnt[100005], num[100005];
int main() {
  cin >> n;
  int maxn = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    cnt[num[x]]--;
    num[x]++;
    maxn = max(maxn, num[x]);
    cnt[num[x]]++;
    int flag = 0;
    if (cnt[1] == i)
      flag = 1;
    else if (cnt[i] == 1)
      flag = 1;
    else if (cnt[1] == 1 && cnt[maxn] * maxn == i - 1)
      flag = 1;
    else if (cnt[maxn] == 1 && cnt[maxn - 1] * (maxn - 1) == i - maxn)
      flag = 1;
    if (flag) ans = i;
  }
  cout << ans << endl;
  return 0;
}
