#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
const int INF = 0x3f3f3f3f;
int color[N], num[N];
bool vis[N];
int a[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans;
  int cnt = 0;
  int kind = 0;
  int maxx = 0;
  int minn = INF;
  for (int i = 1; i <= n; i++) {
    num[a[i]]++;
    color[num[a[i]]]++;
    if (color[num[a[i]]] == 1) {
      cnt++;
    }
    if (color[num[a[i]] - 1]) {
      if (color[num[a[i]] - 1] == 1) {
        color[num[a[i]] - 1] = 0;
        cnt--;
      } else {
        color[num[a[i]] - 1]--;
      }
    }
    if (cnt == 1 && num[a[i]] == 1 || num[a[i]] == i) {
      ans = i;
    }
    if (cnt == 2) {
      if (color[1] == 1) ans = i;
      if (color[num[a[i]]] && color[num[a[i]] - 1] && color[num[a[i]]] == 1)
        ans = i;
      if (color[num[a[i]]] && color[num[a[i]] + 1] && color[num[a[i]] + 1] == 1)
        ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}
