#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a[100010];
  multiset<int> s;
  map<int, int> m;
  int ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    if (m[a[i]] == 1) {
      s.insert(1);
    } else {
      multiset<int>::iterator itr = s.find(m[a[i]] - 1);
      if (itr != s.end()) {
        s.erase(itr);
      }
      s.insert(m[a[i]]);
    }
    multiset<int>::reverse_iterator r = s.rbegin();
    r++;
    multiset<int>::iterator f = s.begin();
    f++;
    if (s.size() == 1 or (*r == *(s.begin()) and *r == *(s.rbegin()) - 1) or
        (*(s.begin()) == 1 and *f == *(s.rbegin()))) {
      ans = i + 1;
    }
  }
  cout << ans;
}
