#include <bits/stdc++.h>
using namespace std;
class DebugStream {
} LOG;
template <typename T>
DebugStream &operator<<(DebugStream &s, const T &) {
  return s;
}
const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
          bs = 29;
int norma[maxN];
map<int, int> inv;
int n, arr[maxN];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (norma[arr[i]]) {
      if (!--inv[norma[arr[i]]]) inv.erase(norma[arr[i]]);
    }
    norma[arr[i]]++;
    inv[norma[arr[i]]]++;
    if (inv.size() >= 3) continue;
    int cabeza = inv.begin()->first, pie = inv.rbegin()->first;
    if ((inv.size() == 1 && inv[cabeza] == 1) ||
        (inv.size() == 1 && cabeza == 1) ||
        (cabeza + 1 == pie && inv[pie] == 1) ||
        (cabeza == 1 && inv[cabeza] == 1)) {
      ans = i + 1;
    }
  }
  cout << ans << '\n';
}
