#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 100100;
int T, n, m;
int a[maxn];
map<int, int> countt;
map<int, int> mp;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    countt[a[i]]++;
  }
  for (auto p : countt) {
    if (p.second != 0) mp[p.second]++;
  }
  int sum = mp.size();
  for (int i = n; i >= 1; i--) {
    if (sum == 2) {
      if (mp[1] == 1) {
        cout << i;
        break;
      } else if (mp[1] == 0) {
        map<int, int>::iterator key = mp.find(1);
        mp.erase(key);
      }
      int t[4];
      int cnt = 0;
      for (auto p : mp) {
        t[cnt++] = p.first;
      }
      if (t[1] - t[0] == 1 && mp[t[1]] == 1) {
        cout << i;
        break;
      }
    }
    if (sum == 1) {
      if (countt[a[i]] == i) {
        cout << i;
        break;
      }
      if (mp[1] != 0) {
        cout << i;
        break;
      } else {
        map<int, int>::iterator key = mp.find(1);
        mp.erase(key);
      }
    }
    int ci = countt[a[i]];
    countt[a[i]]--;
    mp[ci]--;
    if (ci - 1 > 0) mp[ci - 1]++;
    if (ci - 1 > 0 && mp[ci - 1] == 1) sum++;
    if (mp[ci] == 0) {
      map<int, int>::iterator key = mp.find(ci);
      mp.erase(key);
      sum--;
    }
  }
  return 0;
}
