#include <bits/stdc++.h>
using namespace std;
typedef priority_queue<pair<int, int>, vector<pair<int, int> >,
                       greater<pair<int, int> > >
    pq;
long long mod = 1000 * 1000 * 1000 + 7;
int cnt[100010];
int occ_cnt[100010];
int ans = 0;
map<int, int> occ_cnt_map;
int main() {
  int n;
  cin >> n;
  vector<int> input(n);
  for (int i = (0); i < (n); i++) {
    cin >> input[i];
  }
  for (int i = (0); i < (n); i++) {
    int x = input[i];
    if (cnt[x] > 0) {
      occ_cnt[cnt[x]]--;
      occ_cnt_map[cnt[x]]--;
      if (occ_cnt_map[cnt[x]] == 0) {
        occ_cnt_map.erase(cnt[x]);
      }
    }
    cnt[x]++;
    if (cnt[x] > 0) {
      occ_cnt[cnt[x]]++;
      occ_cnt_map[cnt[x]]++;
    }
    if (occ_cnt[1] == i + 1 || occ_cnt[i + 1] == 1) {
      ans = i + 1;
    } else if (occ_cnt[1] == 1 && occ_cnt_map.size() == 2) {
      ans = i + 1;
    } else if (occ_cnt_map.size() == 2) {
      map<int, int>::iterator iter = occ_cnt_map.begin();
      pair<int, int> first_elem = *iter;
      ++iter;
      pair<int, int> second_elem = *iter;
      if (second_elem.first - first_elem.first == 1 &&
          second_elem.second == 1) {
        ans = i + 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
