#include <bits/stdc++.h>
using namespace std;
using lol = long long int;
const lol N = 1e5 + 5;
const lol modcon = 1e9 + 7, mod = 998244353;
const double eps = 1e-7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<bool> ans(N, 0);
  vector<int> cnt(N, 0);
  vector<int> clr(N, 0);
  ans[1] = 1;
  int dsc = 1;
  int u;
  cin >> u;
  cnt[1]++;
  clr[u]++;
  int prc = u;
  for (int i = 2; i <= n; i++) {
    cin >> u;
    if (clr[u] == 0) {
      if (cnt[clr[prc]] == dsc) {
        ans[i] = 1;
      }
      dsc++;
      cnt[1]++;
      clr[u]++;
      if (cnt[1] == dsc - 1 && cnt[2] == 1) {
        ans[i] = 1;
      }
    } else {
      if (cnt[clr[prc]] == dsc) {
        ans[i] = 1;
      }
      cnt[clr[u]]--;
      cnt[clr[u] + 1]++;
      clr[u]++;
      if (cnt[clr[u]] == dsc - 1 && cnt[clr[u] + 1] == 1 && dsc > 1) {
        ans[i] = 1;
      }
      if (cnt[1] == 1 && cnt[clr[u]] == dsc - 1) {
        ans[i] = 1;
      }
    }
  }
  for (int i = n; i >= 0; i--) {
    if (ans[i] == 1) {
      cout << i;
      return 0;
    }
  }
  return 0;
}
