#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  unordered_map<long long, long long> mp1, mp2;
  long long arr[n];
  long long i, j;
  long long a, b, c;
  long long a1, b1, c1, d1;
  long long x = 0;
  for (i = 0; i < n; i++) cin >> arr[i];
  for (i = 0; i < n; i++) {
    a = mp1[arr[i]];
    b = 0;
    if (a != 0) b = mp2[a];
    mp1[arr[i]]++;
    if (b == 1) {
      mp2.erase(a);
    } else if (b > 1) {
      mp2[a]--;
    }
    mp2[mp1[arr[i]]]++;
    if (mp2.size() == 2) {
      auto it = mp2.begin();
      a1 = it->first;
      b1 = it->second;
      it++;
      c1 = it->first;
      d1 = it->second;
      if ((b1 == 1 && (a1 == c1 + 1 || a1 == 1) ||
           (d1 == 1 && (c1 == a1 + 1 || c1 == 1)))) {
        x = i + 1;
      }
    }
    if (mp2.size() == 1) {
      auto it = mp2.begin();
      a1 = it->first;
      b1 = it->second;
      if (a1 == 1 || b1 == 1) {
        x = i + 1;
      }
    }
  }
  cout << x << endl;
}
