#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1e9 + 7;
long long int INF = 1e18;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k = 0, l = 0, r = 0, m = 0, n, b, c, p = 0, cnt = 0,
                      cnt1 = 0, x, y, z, low, mid, high, ans = 0, maxi = 0,
                      mini = INF, t;
  cin >> n;
  long long int A[n];
  long long int B[200005] = {0};
  for (i = 0; i < n; i++) {
    cin >> A[i];
  }
  maxi = -INF;
  mini = INF;
  ans = 0;
  x = 0;
  y = 0;
  l = 0;
  set<long long int> se;
  set<long long int> se1;
  set<long long int> se2;
  map<long long int, long long int> occu;
  for (i = 0; i < n; i++) {
    B[A[i]]++;
    se1.insert(A[i]);
    se2.insert(B[A[i]]);
    occu[B[A[i]]]++;
    if (B[A[i]] != 1) {
      occu[B[A[i]] - 1]--;
      if (occu[B[A[i]] - 1] == 0) se2.erase(B[A[i]] - 1);
    }
    if (B[A[i]] == 1) {
      se.insert(A[i]);
    } else if (B[A[i]] == 2) {
      se.erase(A[i]);
    }
    if (B[A[i]] > maxi && B[A[i]] != 1) {
      maxi = B[A[i]];
      y = 1;
    } else if (B[A[i]] == maxi) {
      y++;
    }
    if (se.size() == 1 && y * maxi == i ||
        se.size() == 0 && se2.size() == 2 && y == 1 &&
            *se2.rbegin() == *(se2.begin()) + 1 ||
        se.size() == i - 1 && maxi == 2 && y == 1 || se.size() == i + 1 ||
        se1.size() == 1) {
      ans = max(ans, i + 1);
    }
  }
  cout << ans << endl;
}
