#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int dp[100005] = {0};
  map<int, int> mp;
  int x;
  int mx = -1;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    int temp = dp[x];
    if (mp.count(temp)) {
      if (mp[temp] == 1) {
        mp.erase(temp);
      } else {
        mp[temp]--;
      }
    }
    dp[x]++;
    temp++;
    if (mp.count(temp)) {
      mp[temp]++;
    } else {
      mp[temp] = 1;
    }
    if (mp.size() == 2) {
      auto it = mp.begin();
      int a = it->first;
      it++;
      int b = it->first;
      if ((a == 1 && mp[a] == 1) || (b - a == 1 && mp[b] == 1)) {
        mx = max(mx, i);
      }
    } else if (mp.size() == 1) {
      auto it = mp.begin();
      int a = it->first;
      if (a == 1 || mp[a] == 1) {
        mx = max(mx, i);
      }
    }
  }
  cout << mx << "\n";
}
