#include <bits/stdc++.h>
using namespace std;
int const MAXN = 1e5 + 10;
int diffSizes = 0;
int acum[MAXN];
int main() {
  int n;
  cin >> n;
  int arr[n + 1];
  for (int i = 1; i <= n; ++i) cin >> arr[i];
  int ans = 1;
  int min1, min2, max1, max2;
  fill(acum, acum + MAXN, 0);
  set<pair<int, int> > s;
  s.insert(pair<int, int>(1, arr[1]));
  acum[arr[1]]++;
  for (int i = 2; i <= n; ++i) {
    int val = ++acum[arr[i]];
    s.erase(pair<int, int>(val - 1, arr[i]));
    s.insert(pair<int, int>(val, arr[i]));
    if (s.size() == 1) {
      ans = i;
      continue;
    }
    max1 = (*--s.end()).first;
    min1 = (*s.begin()).first;
    max2 = min2 = -1;
    if (s.size() == 2) {
      if (min1 == 1 or max1 - min1 == 1) ans = i;
      continue;
    }
    if (s.size() > 2) {
      max2 = (*--(--s.end())).first;
      min2 = (*++s.begin()).first;
      if (min1 > 1) {
        if (max1 - 1 == min1 and max2 == min1) ans = i;
      } else {
        if (max1 - 1 == min1 and max2 == min1) ans = i;
        if (min2 == max1) ans = i;
      }
    }
  }
  cout << ans << endl;
}
