#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:10000000000")
using namespace std;
const int INF = 1000000007;
const long long INF2 = 1000000000000000007;
const long long MOD = 1000000007;
const long long MOD1 = 1864463441;
const int SIZE = 200010;
const double eps = 1e-6;
uniform_int_distribution<int> uid(5, 10);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long n, m, k, t, p, q, a, l, res, r, b, mi, c, c1, cnt, mx, ar[SIZE],
    ar2[SIZE];
bool fl, fl1;
map<long long, long long> mp, mp1;
bool used[SIZE];
set<long long> st;
bool check() {
  c = mp1[1];
  c1 = mp1[*st.rbegin()];
  if ((st.size() == 1 && (*st.begin() == 1 || c1 == 1)) ||
      (st.size() == 2 &&
       ((c == 1) || (*st.rbegin() - *st.begin() == 1 && c1 == 1))))
    return 1;
  else
    return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> ar[i];
    mp[ar[i]]++;
  }
  for (auto& i : mp) {
    mp1[i.second]++;
    st.insert(i.second);
  }
  for (int i = n - 1; i >= 0; i--) {
    if (check()) {
      res = i + 1;
      break;
    }
    mp1[mp[ar[i]]]--;
    if (mp1[mp[ar[i]]] == 0) {
      mp1.erase(mp[ar[i]]);
      st.erase(mp[ar[i]]);
    }
    mp[ar[i]]--;
    if (mp[ar[i]] > 0) {
      mp1[mp[ar[i]]]++;
      st.insert(mp[ar[i]]);
    } else {
      mp.erase(ar[i]);
    }
  }
  cout << res;
  return 0;
}
