#include <bits/stdc++.h>
using namespace std;
int N, M, T, a[100005], ribbon[100005] = {0}, maxi, maxi2, mini, mini2, ans;
string S;
multiset<int> rank1;
void solution1() {
  for (int i = 0; i < N; i++) {
    int old = ribbon[a[i]];
    ribbon[a[i]]++;
    if (old == 0)
      rank1.insert(1);
    else {
      rank1.erase(rank1.find(old));
      rank1.insert(old + 1);
    }
    if (0) {
      cout << "rank1:";
      for (auto r : rank1) cout << r << " ";
      cout << endl;
    }
    auto it = rank1.end();
    if (rank1.size() == 1) {
      ans = i + 1;
      continue;
    }
    it--, maxi = *it;
    it--, maxi2 = *it;
    it = rank1.begin(), mini = *it;
    it++, mini2 = *it;
    if (0) cout << maxi << " " << maxi2 << " " << mini2 << " " << mini << endl;
    if (maxi == maxi2 + 1 && maxi2 == mini) {
    } else if (maxi == mini2 && mini == 1) {
    } else
      continue;
    ans = i + 1;
    if (0) cout << ans << endl;
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> a[i];
  solution1();
  return 0;
}
