#include <bits/stdc++.h>
using namespace std;
bool is_valid(map<int, int> &cnt) {
  if (cnt.size() == 3) {
    auto it = cnt.begin();
    for (int i = 0; i < 2; ++i) {
      if ((++it)->second == 1 && cnt.find(it->first - 1) != cnt.end()) {
        return true;
      }
    }
  }
  auto it = next(cnt.begin());
  return cnt.size() == 2 &&
         (it->second == 1 || cnt.find(it->first - 1) != cnt.end());
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> freq(100010);
  map<int, int> cnt;
  for (int i = 0; i < freq.size(); ++i) {
    ++cnt[freq[i]];
  }
  int ans = 0;
  for (int i = 0, x; i < n; ++i) {
    cin >> x;
    if (--cnt[freq[x]] == 0) {
      cnt.erase(freq[x]);
    }
    ++cnt[++freq[x]];
    if (is_valid(cnt)) {
      ans = i + 1;
    }
  }
  cout << ans << "\n";
}
