#include <bits/stdc++.h>
using namespace std;
const int MAXn = 1e5 + 10;
int cnt[MAXn], ans, c[MAXn], ar[MAXn], n, hp;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> ar[i];
  for (int i = 0; i < n; i++) {
    if (i > 0 && hp <= 2) {
      if (((cnt[1] == 1) || (hp == 1 && cnt[1]) ||
           (cnt[c[ar[i - 1]]] == 1 && hp == 1)) ||
          ((cnt[c[ar[i - 1]]] == 1 && cnt[abs(c[ar[i - 1]] - 1)]) ||
           (cnt[c[ar[i - 1]]] && cnt[abs(c[ar[i - 1]] + 1)] == 1)))
        ans = i;
    }
    c[ar[i]]++;
    if (c[ar[i]] == 1) {
      if (cnt[1] == 0) {
        hp++;
      }
      cnt[1]++;
    } else {
      if (cnt[c[ar[i]] - 1] == 1) {
        hp--;
      }
      cnt[c[ar[i]] - 1]--;
      if (cnt[c[ar[i]]] == 0) hp++;
      cnt[c[ar[i]]]++;
    }
  }
  if (hp <= 2) {
    int i = n - 1;
    if (((cnt[1] == 1) || (hp == 1 && cnt[1]) ||
         (cnt[c[ar[i]]] == 1 && hp == 1)) ||
        ((cnt[c[ar[i]]] == 1 && cnt[abs(c[ar[i]] - 1)]) ||
         (cnt[c[ar[i]]] && cnt[abs(c[ar[i]] + 1)] == 1)))
      ans = n;
  }
  cout << ans;
}
