#include <bits/stdc++.h>
using namespace std;
long long solve() {
  long long n;
  cin >> n;
  vector<long long> arr(n);
  map<long long, long long> counts;
  map<long long, long long> checks;
  for (auto &x : arr) {
    cin >> x;
    counts[x]++;
  }
  for (auto x : counts) {
    checks[x.second]++;
  }
  while (n--) {
    if (checks.size() == 1) {
      auto x = checks.begin();
      if (x->first == 1 or x->second == 1) {
        cout << n + 1 << endl;
        return 0;
      }
    }
    if (checks.size() == 2) {
      auto it1 = checks.begin();
      auto it2 = checks.begin();
      if (it1->first == 1 and it1->second == 1) {
        cout << n + 1 << endl;
        return 0;
      }
      it2++;
      if (it2->first - it1->first == 1 and it2->second == 1) {
        cout << n + 1 << endl;
        return 0;
      }
    }
    long long x = counts[arr[n]];
    if (x) {
      checks[x]--;
      counts[arr[n]]--;
      if (x - 1) checks[x - 1]++;
      if (!checks[x]) checks.erase(x);
    }
  }
}
signed main() {
  long long t = 1;
  while (t--) {
    solve();
  }
}
