#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a;
  int x;
  set<int> set1;
  for (int i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
    set1.insert(x);
  }
  if (set1.size() == 1) {
    cout << n;
    return 0;
  }
  map<int, int> mp, mp2;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    x = a[i];
    if (mp.find(mp2[a[i]]) != mp.end() && mp[mp2[a[i]]] > 0) {
      mp[mp2[a[i]]]--;
      if (mp[mp2[a[i]]] == 0) mp.erase(mp2[a[i]]);
    }
    x = a[i];
    mp2[a[i]]++;
    x = mp2[a[i]];
    mp[mp2[a[i]]]++;
    if (mp.size() == 2) {
      int x = mp.begin()->second;
      int y = mp.rbegin()->second;
      if (x == 1 && (mp.begin()->first == 1 ||
                     mp.begin()->first == mp.rbegin()->first + 1))
        ans = i + 1;
      if (y == 1 && (mp.rbegin()->first == 1 ||
                     mp.rbegin()->first == mp.begin()->first + 1))
        ans = i + 1;
    }
    if (mp.size() == 1) {
      int x = mp.begin()->second;
      int y = mp.rbegin()->second;
      if (x == y && mp.begin()->first == 1) ans = i + 1;
    }
  }
  cout << ans;
  return 0;
}
