#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
int mod = 1000000007;
db pie = 3.1415926535;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int a[n];
  int i;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  map<int, int> mp1;
  for (i = 0; i < n; i++) {
    mp1[a[i]]++;
  }
  map<int, int> mp2;
  for (auto it = mp1.begin(); it != mp1.end(); it++) {
    mp2[it->second]++;
  }
  int mx = 0;
  for (i = n - 1; i >= 0; i--) {
    if (mp2.size() == 2) {
      auto it = mp2.begin();
      auto it2 = it;
      it++;
      auto it3 = it;
      if ((it3->first - it2->first == 1 && it3->second == 1) ||
          (it2->first == 1 && it2->second == 1)) {
        if (i + 1 > mx) {
          mx = i + 1;
          break;
        }
      }
    } else if (mp2.size() == 1) {
      auto it = mp2.begin();
      if (it->first == 1 || it->second == 1) {
        if (i + 1 > mx) {
          mx = i + 1;
          break;
        }
      }
    }
    int val = mp1[a[i]];
    mp1[a[i]]--;
    mp2[val]--;
    if (val - 1 > 0) mp2[val - 1]++;
    if (mp2[val] == 0) {
      mp2.erase(val);
    }
  }
  cout << mx << "\n";
}
