#include <bits/stdc++.h>
using namespace std;
long long Ceil(long long a, long long b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long inv(long long a) {
  long long p = 1000000007;
  long long b = p - 2, ans = 1;
  for (; b > 0; a = a * a % p, b >>= 1)
    if (b & 1) ans = ans * a % p;
  return ans;
}
long long ncrmod(long long n, long long m) {
  long long p = 1000000007;
  if (m > n) return 0;
  long long up = 1, down = 1;
  for (long long i = n - m + 1; i < n + 1; i++) up *= i, up %= p;
  for (long long i = 1; i < m + 1; i++) down *= i, down %= p;
  return up * inv(down) % p;
}
long long powmod(long long a, long long n) {
  if (!n) return 1;
  long long pt = powmod(a, n / 2);
  pt *= pt, pt %= 1000000007;
  if (n & 1) pt *= a, pt %= 1000000007;
  return pt;
}
void factorize(long long int n) {
  long long sum2 = 0, sum = 0;
  long long count = 0;
  while (!(n % 2)) {
    n >>= 1;
    count++;
  }
  if (count) {
    sum2 += count;
  }
  for (long long i = 3; i <= sqrt(n); i += 2) {
    count = 0;
    while (n % i == 0) {
      count++;
      n = n / i;
    }
    if (count) {
      if (i & 1) sum += count;
    }
  }
  if (n > 2) {
    sum++;
  }
}
void solve() {
  long long n;
  cin >> n;
  long long a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  unordered_map<long long, long long> m, m2;
  for (long long i = 0; i < n; i++) {
    m[a[i]]++;
  }
  for (auto i : m) {
    m2[i.second]++;
  }
  for (long long i = n - 1; i >= 0; i--) {
    if (m.size() == 1 or (m2.size() == 1 and m2.begin()->first == 1)) {
      cout << i + 1;
      return;
    } else if (m2.size() == 2) {
      auto it = m2.begin();
      it++;
      if (it->first - m2.begin()->first == 1 and it->second == 1) {
        cout << i + 1;
        return;
      } else if (m2.begin()->first - it->first == 1 and
                 m2.begin()->second == 1) {
        cout << i + 1;
        return;
      } else if ((it->second == 1 and it->first == 1) or
                 (m2.begin()->second == 1 and m2.begin()->first == 1)) {
        cout << i + 1;
        return;
      }
    }
    if (m[a[i]] - 1 > 0) m2[m[a[i]] - 1]++;
    m2[m[a[i]]]--;
    if (m2[m[a[i]]] == 0) m2.erase(m[a[i]]);
    m[a[i]]--;
    if (m[a[i]] == 0) m.erase(m[a[i]]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
