#include <bits/stdc++.h>
using namespace std;
int a[1000000];
int b[1000000];
int main() {
  int n;
  cin >> n;
  int i, j;
  map<int, int> m;
  int maxi = 1;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (m.count(a[i]) > 0)
      m[a[i]]++;
    else
      m[a[i]] = 1;
    maxi = max(maxi, m[a[i]]);
    b[i] = maxi;
  }
  int k = -1;
  int nn;
  auto it = m.begin();
  for (i = n - 1; i >= 0; i--) {
    if (m.size() == 1) {
      cout << i + 1;
      return 0;
    }
    if (m.size() == i + 1) {
      cout << i + 1;
      return 0;
    }
    nn = i;
    maxi = 0;
    if (i % (m.size() - 1) == 0) {
      nn = i / (m.size() - 1);
      k = -1;
      for (it = m.begin(); it != m.end(); it++) {
        if (it->second == 1 && k == -1) {
          k = 0;
          continue;
        }
        if (it->second != nn) break;
      }
      if (it == m.end() && k == 0) {
        cout << i + 1;
        return 0;
      }
    }
    k = -1;
    if ((i + 1 - b[i]) % (m.size() - 1) == 0) {
      nn = ((i + 1 - b[i]) / (m.size() - 1));
      for (it = m.begin(); it != m.end(); it++) {
        if (it->second == nn + 1 && k == -1) {
          k = 0;
          continue;
        }
        if (it->second != nn) break;
      }
      if (it == m.end() && k == 0) {
        cout << i + 1;
        return 0;
      }
    }
    if (m[a[i]] == 1)
      m.erase(a[i]);
    else
      m[a[i]]--;
  }
  return 0;
}
