#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int freq[100001 + 1];
  int cnt[100001 + 1];
  int ans = 0;
  memset(freq, 0, sizeof(freq));
  memset(cnt, 0, sizeof(cnt));
  int mx = 0;
  for (int i = 0; i < n; i++) {
    cnt[freq[a[i]]]--;
    freq[a[i]]++;
    cnt[freq[a[i]]]++;
    mx = max(mx, freq[a[i]]);
    int vool = 0;
    if (cnt[1] == i + 1)
      vool = 1;
    else if (cnt[i + 1] == 1)
      vool = 1;
    else if (cnt[mx] * mx == (i + 1 - 1) && cnt[1] == 1)
      vool = 1;
    else if (cnt[mx] == 1 && cnt[mx - 1] * (mx - 1) == (i + 1 - mx))
      vool = 1;
    if (vool) ans = i + 1;
  }
  cout << ans;
}
