#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long INF = 1e18;
int n;
int a[maxn];
int cnt[maxn], f[maxn];
void ReadInput() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
}
void Solve() {
  int cur = 0;
  int ans = 0;
  int mx = 0;
  for (int i = 1; i <= n; ++i) {
    ++cnt[a[i]];
    --f[cnt[a[i]] - 1];
    ++f[cnt[a[i]]];
    if (cnt[a[i]] == 1) ++cur;
    mx = max(cnt[a[i]], mx);
    if (cur == 1) ans = i;
    if (mx == 1) ans = i;
    if (mx * f[mx] == i - 1) ans = i;
    if (f[mx] == 1 && (mx - 1) * f[mx - 1] + mx == i) ans = i;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ReadInput();
  Solve();
}
