#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
int a[N], cnt[N], cntt[N];
int n;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = (1); i <= (n); i++) {
    cin >> a[i];
  }
  int ans = -1;
  set<int> stf;
  cntt[0] = n;
  for (int i = (1); i <= (n); i++) {
    cntt[cnt[a[i]]]--;
    if (cnt[a[i]] > 0 && cntt[cnt[a[i]]] == 0) {
      stf.erase(stf.find(cnt[a[i]]));
    }
    cnt[a[i]]++;
    cntt[cnt[a[i]]]++;
    if (cntt[cnt[a[i]]] == 1) {
      stf.insert(cnt[a[i]]);
    }
    if (stf.size() == 1) {
      int f = *(stf.begin());
      if (f == 1 || cntt[f] == 1) {
        ans = i;
      }
    } else if (stf.size() == 2) {
      int f = *(stf.begin()), g = *(stf.rbegin());
      if (f == 1 && cntt[f] == 1) {
        ans = i;
      } else if (f == g - 1 && cntt[g] == 1) {
        ans = i;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
