#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];
  bool allsame = true;
  for (int i = 1; i < n; i++)
    if (a[i] != a[i - 1]) allsame = false;
  if (allsame) {
    cout << n;
    return 0;
  }
  vector<int> cnt((int)(1e5 + 1), 0);
  vector<int> cntcnt((int)(1e5 + 1), 0);
  set<int> s;
  int cmax = 0;
  for (int i = 0; i < n; i++) {
    cnt[a[i]]++;
    cntcnt[cnt[a[i]]]++;
    s.insert(cnt[a[i]]);
    if (cntcnt[cnt[a[i]] - 1] != 0) cntcnt[cnt[a[i]] - 1]--;
    if (cntcnt[cnt[a[i]] - 1] == 0) s.erase(cnt[a[i]] - 1);
    if (s.size() == 1) {
      if (cntcnt[1] != 0) cmax = max(cmax, i + 1);
    }
    if (s.size() == 2) {
      if (cntcnt[1] == 1) {
        cmax = max(cmax, i + 1);
      }
      int cntt;
      for (auto x : s) {
        cntt = x;
        break;
      }
      if ((cntcnt[cntt + 1] == 1) ||
          (cntcnt[cntt] == 1 && (cntcnt[cntt - 1] != 0))) {
        cmax = max(cmax, i + 1);
      }
    }
  }
  cout << cmax;
}
