#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  map<int, int> d;
  set<int> s;
  map<int, set<int>> r;
  set<int> c;
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int x = a[i];
    c.insert(x);
    auto it = r[d[x]].find(x);
    if (it != r[d[x]].end()) {
      r[d[x]].erase(it);
    }
    if (r[d[x]].size() == 0) {
      it = s.find(d[x]);
      if (it != s.end()) {
        s.erase(it);
      }
    }
    d[x]++;
    r[d[x]].insert(x);
    s.insert(d[x]);
    if (s.size() == 1 && (*s.begin() == 1 || c.size() == 1)) {
      ans = i + 1;
      continue;
    }
    if (s.size() == 2) {
      int r1 = *s.begin();
      it = s.begin();
      ++it;
      int r2 = *it;
      if ((r[r1].size() == 1 &&
           (d[*r[r1].begin()] == 1 || d[*r[r1].begin()] - 1 == r2)) ||
          (r[r2].size() == 1 &&
           (d[*r[r2].begin()] == 1 || d[*r[r2].begin()] - 1 == r1))) {
        ans = i + 1;
      }
    }
  }
  cout << ans;
}
