#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double Pi = 3.141592653589793116L;
const long double EPS = 0.000000001L, Gold = ((1.0L + sqrt(5.0L)) / 2.0L);
long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
int OImode = 0;
int MultiTest = 0;
int Interactive = 0;
void ConfigMonitor();
char infile[] = "FILE.IN";
char outfile[] = "FILE.OUT";
int n;
vector<int> a;
int cnt[100001];
map<int, int> Map;
bool validMap() {
  if (Map.size() == 1 && (Map.begin()->first == 1 || Map.begin()->second == 1))
    return true;
  if (Map.size() != 2) return false;
  if (Map.size() == 2 && Map.begin()->first != 1) {
    int cnt1 = Map.begin()->second, cnt2 = Map.rbegin()->second;
    if ((cnt1 == cnt2 || cnt2 == 1) &&
        Map.rbegin()->first - Map.begin()->first == 1)
      return true;
    return false;
  }
  int cnt1 = Map[1], cnt2 = Map.rbegin()->second;
  if (cnt1 == 1) return true;
  if (Map.rbegin()->first == 2 && cnt2 == 1) return true;
  return false;
}
void Input() {
  cin >> n;
  a.resize(n);
  for (auto& z : a) cin >> z;
}
void Solve() {
  int x = 0;
  for (int i = 0; i < n; i++) {
    if (cnt[a[i]]) {
      Map[cnt[a[i]]]--;
      if (!Map[cnt[a[i]]]) Map.erase(cnt[a[i]]);
    }
    cnt[a[i]]++;
    Map[cnt[a[i]]]++;
    if (validMap()) x = max(x, i + 1);
  }
  cout << x << '\n';
}
int main(int argc, char* argv[]) {
  ConfigMonitor();
  ios_base::sync_with_stdio(false);
  if (!Interactive) cin.tie(NULL);
  int T = 1;
  if (MultiTest) cin >> T;
  while (T--) {
    Input();
    Solve();
  }
  return 0;
}
void ConfigMonitor() {
  if (OImode) {
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
  }
}
