#include <bits/stdc++.h>
using namespace std;
signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen("A.INP", "r")) {
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  if (fopen(""
            ".in",
            "r")) {
    freopen(
        ""
        ".in",
        "r", stdin);
    freopen(
        ""
        ".out",
        "w", stdout);
  }
  int n;
  cin >> n;
  vector<int> cnt(1e5 + 5, 0);
  vector<int> same(1e5 + 5, 0);
  int res = 0;
  int sz = 0;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    same[cnt[x]]--;
    cnt[x]++;
    same[cnt[x]]++;
    sz = max(sz, cnt[x]);
    if (same[1] == i)
      res = i;
    else if (same[i] == 1)
      res = i;
    else if (same[1] == 1 && same[sz] * sz == i - 1)
      res = i;
    else if (same[sz - 1] * (sz - 1) == i - sz && same[sz] == 1)
      res = i;
  }
  cout << res << '\n';
}
