#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const long long INF = 1e18L;
long long ar[N];
long long tree[2 * N];
long long dp[N];
long long cnt[N];
long long cnt2[N];
long long d;
bool check(long long x) {
  return (cnt2[x] == d - 1 && (cnt2[x + 1] == 1 || cnt2[1] == 1));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long n;
  cin >> n;
  d = 0;
  long long ans = 1;
  multiset<long long> cnt2s;
  for (long long int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    if (!cnt[x]) {
      ++d;
      ++cnt2[0];
      cnt2s.insert(0);
    }
    cnt2s.erase(cnt2s.find(cnt[x]));
    cnt2[cnt[x]]--;
    cnt[x]++;
    cnt2[cnt[x]]++;
    cnt2s.insert(cnt[x]);
    if (d == 1 || cnt2[1] == d || check(1) || check(*cnt2s.begin()) ||
        check(*cnt2s.rbegin()))
      ans = max(ans, i + 1);
  }
  cout << ans;
  return 0;
}
