#include <bits/stdc++.h>
using namespace std;
using namespace std;
const long long N = 2e5 + 5, M = 1e9 + 7;
const unsigned long long base = 13331;
const double Pi = acos(-1.0);
const long long C = 299792458;
int h[N];
inline int lowbit(int x) { return x & (-x); }
int sum(int x) {
  int ans = 0;
  for (int i = x; i >= 1; i -= lowbit(i)) ans = ans + h[i];
  return ans;
}
void add(int x, int y) {
  for (int i = x; i <= N; i += lowbit(i)) h[i] = h[i] + y;
}
int a[N], cnt[N];
int n;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int maxx = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (cnt[a[i]]) add(cnt[a[i]], -1);
    cnt[a[i]]++;
    maxx = max(maxx, cnt[a[i]]);
    add(cnt[a[i]], 1);
    if (maxx == 1) {
      ans = max(ans, i);
      continue;
    }
    int sum2 = sum(maxx - 2);
    int sum1 = sum(maxx - 1);
    int sum0 = sum(maxx);
    if (sum0 == 1) {
      ans = max(ans, i);
      continue;
    }
    if (sum1 == 1 && sum(1) - sum(0) == 1) {
      ans = max(ans, i);
      continue;
    }
    if (sum0 - sum1 == 1 && sum2 == 0) {
      ans = max(ans, i);
      continue;
    }
  }
  cout << ans << endl;
}
