#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long isPrime(long long n) {
  long long p = (long long)sqrt(n);
  for (long long i = (2); i <= (p); i += (1))
    if (n % i == 0) return 0;
  return 1;
}
long long pows(long long b, long long e) {
  if (e == 0)
    return 1;
  else if (e % 2 == 0) {
    long long a = pow(b, e / 2);
    return a * a;
  } else {
    long long a = pow(b, e / 2);
    return b * a * a;
  }
}
long long powm(long long a, long long b, long long m = 1000000007) {
  long long x = 1;
  a %= 1000000007;
  while (b) {
    if (b & 1) x = x * a % 1000000007;
    a = a * a % 1000000007;
    b >>= 1;
  }
  return x;
}
long long modInverse(long long a, long long m = 1000000007) {
  return powm(a, m - 2, m);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m, i, j, k, x, y, z, f, p, q, g, l, r, w, h,
      count = 0, count1 = 0, prod = 1, a, b, c, d, index, x1, x2, diff, ans = 0,
      sum = 0, sum1 = 0, sum2 = 0, flag = 0, flag1 = 0, flag2 = 0;
  string s, s1, s2;
  cin >> n;
  long long ar[n];
  for (i = 0; i < n; i++) {
    cin >> ar[i];
  }
  unordered_map<long long, long long> m1, m2;
  for (i = 0; i < n; i++) {
    m1[ar[i]]++;
  }
  for (auto i : m1) {
    m2[i.second]++;
  }
  if (m1.size() == 1 || m1.size() == n) {
    cout << n << "\n";
    return 0;
  } else {
    ans = 1;
    for (i = n - 1; i >= 0; i--) {
      if (m2.size() == 2 && m2[1] == 1) {
        ans = max(ans, i + 1);
      }
      if (m2[1] == 0) {
        m2.erase(1);
      }
      if (m2.size() == 2) {
        vector<pair<long long, long long>> v;
        for (auto i : m2) {
          v.push_back(make_pair(i.first, i.second));
        }
        if (v[0].first < v[1].first) {
          swap(v[0], v[1]);
        }
        if ((v[0].first - v[1].first == 1 && v[0].second == 1)) {
          ans = max(ans, i + 1);
        }
      }
      m2[m1[ar[i]]]--;
      if (m1[ar[i]] > 1) {
        m2[m1[ar[i]] - 1]++;
      }
      if (m2[m1[ar[i]]] == 0) {
        m2.erase(m1[ar[i]]);
      }
      m1[ar[i]]--;
      if (m1[ar[i]] == 0) {
        m1.erase(ar[i]);
      }
    }
    cout << ans;
  }
}
