#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  int arr[n + 10];
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
  }
  int freq[1000 * 100 + 7];
  memset(freq, 0, sizeof freq);
  set<int> df;
  map<int, int> qtdf;
  int bestX = 1;
  freq[arr[1]]++;
  df.insert(1);
  qtdf[1]++;
  for (int i = 2; i <= n; i++) {
    if (qtdf[freq[arr[i]]] == 1) {
      df.erase(freq[arr[i]]);
    }
    qtdf[freq[arr[i]]]--;
    freq[arr[i]]++;
    qtdf[freq[arr[i]]]++;
    df.insert(freq[arr[i]]);
    if (df.size() > 2) continue;
    if (df.size() == 2) {
      vector<int> aux;
      for (auto a : df) {
        aux.push_back(a);
      }
      sort(aux.begin(), aux.end());
      if (aux[1] - 1 == aux[0] && qtdf[aux[1]] == 1) bestX = i;
      if (aux[0] == 1 && qtdf[aux[0]] == 1) bestX = i;
    } else {
      vector<int> aux;
      for (auto a : df) {
        aux.push_back(a);
      }
      if (aux[0] == 1 || qtdf[aux[0]] == 1) bestX = i;
    }
  }
  printf("%d\n", bestX);
  return 0;
}
