#include <bits/stdc++.h>
using namespace std;
map<int, int> counts;
int a[100010];
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int ans = -1;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    if (a[x] != 0) counts[a[x]]--;
    counts[a[x] + 1]++;
    a[x]++;
    if (counts[a[x] - 1] == 0) {
      counts.erase(a[x] - 1);
    }
    if (counts.size() == 1 &&
        (counts.begin()->second == 1 ||
         (counts.find(1) != counts.end() && counts[1] > 0))) {
      ans = i;
    }
    if (counts.size() == 2) {
      auto it = counts.begin();
      pair<int, int> left = *it;
      it++;
      pair<int, int> right = *it;
      if (left.first + 1 == right.first && right.second == 1) {
        ans = i;
      }
      if (left.first == 1 && left.second == 1) {
        ans = i;
      }
      if (left.first == 1 && left.second == right.second + 1 &&
          right.second == 1) {
        ans = i;
      }
    }
  }
  cout << ans + 1;
}
