#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e6 + 10;
const int inf = (int)2e9;
int n, col[N], alive[3 * N], sum[3 * N];
void build(int node, int b, int e) {
  if (b == e) {
    alive[node] = 0, sum[node] = 0;
    return;
  }
  int lft = 2 * node, rt = lft + 1, mid = (b + e) / 2;
  build(lft, b, mid);
  build(rt, mid + 1, e);
  alive[node] = (alive[lft] + alive[rt]);
}
void upd(int node, int b, int e, int i, int j, int v) {
  if (e < i || b > j) return;
  if (b >= i && e <= j) {
    sum[node] += v;
    if (sum[node] == 1) alive[node] = 1;
    if (sum[node] == 0) alive[node] = 0;
    if (sum[node] <= 0) sum[node] = 0;
    return;
  }
  int lft = 2 * node, rt = lft + 1, mid = (b + e) / 2;
  upd(lft, b, mid, i, j, v);
  upd(rt, mid + 1, e, i, j, v);
  alive[node] = (alive[lft] + alive[rt]);
}
pair<int, int> kth(int node, int b, int e, int k) {
  if (b == e) {
    return {b, sum[node]};
  }
  int lft = 2 * node, rt = lft + 1, mid = (b + e) / 2;
  if (k > alive[lft]) {
    return kth(rt, mid + 1, e, k - alive[lft]);
  } else {
    return kth(lft, b, mid, k);
  }
}
int cnt[N], answer = 0;
void update_ans(int i) {
  if (alive[1] == 1) {
    auto p = kth(1, 1, n, 1);
    if (p.second == 1) {
      answer = i;
    }
    if (p.first == 1) {
      answer = i;
    }
  }
  if (alive[1] == 2) {
    auto p = kth(1, 1, n, 1);
    auto q = kth(1, 1, n, 2);
    if (p.second == 1) {
      if (p.first == q.first + 1) {
        answer = i;
      }
    }
    if (q.second == 1) {
      if (q.first == p.first + 1) {
        answer = i;
      }
    }
    if (p.first == 1 && p.second == 1) answer = i;
    if (q.first == 1 && q.second == 1) answer = i;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &col[i]);
  }
  memset(cnt, 0, sizeof(cnt));
  for (int i = 1; i <= n; i++) {
    if (cnt[col[i]] > 0) upd(1, 1, n, cnt[col[i]], cnt[col[i]], -1);
    ++cnt[col[i]];
    upd(1, 1, n, cnt[col[i]], cnt[col[i]], 1);
    update_ans(i);
  }
  printf("%d\n", answer);
  return 0;
}
