#include <bits/stdc++.h>
using namespace std;
int power(int x, int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
map<int, int> m, check;
set<int> s, freq;
bool valid(int j) {
  if (freq.size() > 2) return 0;
  if (freq.size() == 1 &&
      (check[*freq.begin()] == 1 ||
       (*freq.begin() == 1 && check[*freq.begin()] == j + 1)))
    return true;
  else if (freq.size() == 1)
    return false;
  int f = *freq.begin(), sec = *freq.rbegin();
  if ((sec == f + 1 && check[sec] == 1) || (f == 1 && check[1] == 1))
    return true;
  return false;
}
int32_t main() {
  int n, mmax = -1;
  cin >> n;
  vector<int> v(n), x(100001);
  for (int i = 0; i < n; i++) cin >> v[i];
  for (int i = 0; i < n; i++) {
    if (m[v[i]] > 0) {
      check[m[v[i]]]--;
      if (check[m[v[i]]] == 0) freq.erase(m[v[i]]);
    }
    m[v[i]]++;
    freq.insert(m[v[i]]);
    check[m[v[i]]]++;
    if (valid(i)) mmax = max(mmax, i + 1);
  }
  cout << mmax;
  return 0;
}
