#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int da[maxn] = {0};
map<int, int> cnt;
int main() {
  int n, m;
  cin >> n;
  int inp;
  int num = 0;
  int mmax = 1;
  map<int, int>::iterator idx;
  for (int i = 1; i <= n; i++) {
    cin >> inp;
    if (da[inp] != 0) {
      cnt[da[inp]]--;
      if (cnt[da[inp]] == 0) cnt.erase(da[inp]);
    }
    da[inp]++;
    cnt[da[inp]]++;
    if (cnt.size() > 2) continue;
    if (cnt.size() == 1) {
      if (cnt.begin()->first == 1 || cnt.begin()->second == 1) mmax = i;
      continue;
    }
    idx = cnt.begin();
    int cnt1 = idx->second, num1 = idx->first;
    idx++;
    int cnt2 = idx->second, num2 = idx->first;
    if (cnt1 == 1 && num1 == 1) mmax = i;
    if (cnt2 == 1 && num2 == num1 + 1) mmax = i;
  }
  cout << mmax << endl;
  return 0;
}
