#include <bits/stdc++.h>
using namespace std;
int n;
int arr[100003];
void read() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> arr[i];
}
int cnt[100003];
int cnt_cnt[100003];
multiset<int> s;
void solve() {
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cnt_cnt[cnt[arr[i]]]--;
    if (s.find(cnt[arr[i]]) != s.end()) s.erase(s.find(cnt[arr[i]]));
    cnt[arr[i]]++;
    cnt_cnt[cnt[arr[i]]]++;
    s.insert(cnt[arr[i]]);
    int el = *prev(s.end());
    if (el * cnt_cnt[el] == i - 1 && cnt_cnt[1] == 1) ans = i;
    if (cnt_cnt[el] == 1 && cnt_cnt[el - 1] * (el - 1) == i - el) ans = i;
    if (cnt_cnt[el] == 1 && el == i) ans = i;
    if (el == 1) ans = i;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  read();
  solve();
}
