#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
int a[N];
int num[N], minn, nxt, maxn;
int List[N];
int kind;
bool Judge() {
  if (kind == 1) return 1;
  if (minn == 1 && nxt == maxn) return 1;
  if (minn == maxn) return 0;
  if (maxn > minn + 1) return 0;
  if (maxn == minn + 1 && List[maxn] == 1)
    return 1;
  else
    return 0;
}
int main() {
  scanf("%d", &n);
  minn = 1e6;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (num[a[i]] == 0) kind++;
    num[a[i]]++;
    if (num[a[i]] > maxn) maxn = num[a[i]];
  }
  for (int i = 1; i <= 1e5; i++)
    if (num[i] != 0) {
      List[num[i]]++;
      minn = min(minn, num[i]);
    }
  if (kind == 1) {
    printf("%d", n);
    return 0;
  }
  if (kind == 2) {
    int i;
    for (i = n; i >= 1; i--) {
      if (minn == 1) break;
      if (minn + 1 == maxn) break;
      List[num[a[i]]]--;
      List[num[a[i]] - 1]++;
      if (List[maxn] == 0) maxn--;
      if (List[minn - 1] != 0) minn--;
      num[a[i]]--;
    }
    printf("%d", i);
    return 0;
  }
  if (List[minn] != 1)
    nxt = minn;
  else
    for (int i = minn + 1; i <= maxn; i++)
      if (List[i] != 0) {
        nxt = i;
        break;
      }
  for (int i = n; i >= 1; i--) {
    if (Judge()) {
      printf("%d", i);
      break;
    }
    List[num[a[i]]]--;
    if (num[a[i]] != 1)
      List[num[a[i]] - 1]++;
    else
      kind--;
    if (num[a[i]] != 1) {
      if (minn != nxt) {
        if (minn == num[a[i]]) minn--;
        if (nxt == num[a[i]]) nxt--;
      } else if (minn == num[a[i]])
        minn--;
      if (List[maxn] == 0) maxn--;
    } else {
      minn = nxt;
      if (List[nxt] == 1) {
        for (int j = minn + 1; j <= maxn; j++)
          if (List[j] != 0) {
            nxt = j;
            break;
          }
      }
    }
    num[a[i]]--;
    if (kind == 2) nxt = maxn;
  }
}
