#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 1e5 + 5;
int a[N], cnt[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  multiset<int> s;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    cnt[a[i]] = -1;
  }
  for (int i = 1; i <= n; ++i) {
    if (cnt[a[i]] == -1) {
      cnt[a[i]] = 0;
      s.insert(0);
    }
  }
  int ans = 0, mx = 0, cur = 0, was = 0;
  for (int i = 1; i <= n; ++i) {
    s.erase(s.find(cnt[a[i]]));
    cnt[a[i]]++;
    s.insert(cnt[a[i]]);
    if (cnt[a[i]] == 1) cur++;
    if (mx < cnt[a[i]]) {
      was = mx;
      mx = cnt[a[i]];
    }
    if ((s.count(mx) == 1 && s.count(was) == cur - 1) ||
        (s.count(1) == 1 && s.count(mx) == cur - 1) || s.count(1) == cur)
      ans = i;
  }
  cout << ans;
}
