#include <bits/stdc++.h>
using namespace std;
int INF = (int)(1e6);
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> c((int)1e5 + 113);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    c[a[i]]++;
  }
  reverse(a.begin(), a.end());
  vector<int> amount((int)(1e5) + 113);
  set<int> different;
  for (int i = 0; i < c.size(); ++i) {
    if (c[i] != 0) {
      amount[c[i]]++;
      different.insert(c[i]);
    }
  }
  for (int i = 0; i <= n; ++i) {
    if (different.size() == 1) {
      int u = *(different.begin());
      if (amount[u] == 1 || u == 1) {
        cout << n - i;
        return 0;
      }
    }
    if (different.size() == 2) {
      int u = *different.begin();
      different.erase(different.begin());
      int v = *different.begin();
      different.erase(different.begin());
      if (abs(u - v) == 1 && (u > v ? amount[u] == 1 : amount[v] == 1)) {
        cout << n - i;
        return 0;
      } else if ((u == 1 && amount[u] == 1) || (v == 1 && amount[v] == 1)) {
        cout << n - i;
        return 0;
      }
      different.insert(u);
      different.insert(v);
    }
    if (amount[c[a[i]]] - 1 == 0) different.erase(c[a[i]]);
    amount[c[a[i]]]--;
    c[a[i]]--;
    if (c[a[i]] != 0) {
      amount[c[a[i]]]++;
      different.insert(c[a[i]]);
    }
  }
}
