#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
const int LOG = 21;
const int CNTSZ = 30;
const int INF = 2e9;
const int MOD = 16714589;
const int D = 320;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  map<int, int> color, cnt;
  vector<int> a(n);
  for (int i = 0; i < int(n); i++) cin >> a[i];
  int mx = -1, ans;
  for (int i = 0; i < int(n); i++) {
    cnt[color[a[i]]]--;
    color[a[i]]++;
    cnt[color[a[i]]]++;
    mx = max(mx, color[a[i]]);
    if (cnt[1] == i + 1 || cnt[i + 1] == 1 ||
        (cnt[1] == 1 && cnt[mx] * mx == i) ||
        ((cnt[mx - 1]) * (mx - 1) == i + 1 - mx && cnt[mx] == 1)) {
      ans = i;
    }
  }
  cout << ans + 1 << "\n";
  return 0;
}
