#include <bits/stdc++.h>
using namespace std;
const long long INF = LLONG_MAX;
const long long mod = 1000000007;
long long custom_ceil(long long Nr, long long Dr) {
  if (Nr % Dr == 0)
    return Nr / Dr;
  else
    return Nr / Dr + 1;
}
void solve() {
  long long n;
  cin >> n;
  long long a[n];
  map<long long, long long> mp;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }
  map<long long, set<long long>> mpv;
  for (auto x : mp) {
    mpv[x.second].insert(x.first);
  }
  long long f = 0;
  for (long long i = n - 1; i >= 1; i--) {
    if ((int)((mpv).size()) == 2) {
      auto ite = mpv.rbegin();
      auto itb = mpv.begin();
      long long a = (int)((itb->second).size());
      long long b = (int)((ite->second).size());
      long long af = itb->first;
      long long bf = ite->first;
      if (a == 1 && af == 1) {
        cout << i + 1 << endl;
        return;
      } else if (b == 1 && bf - 1 == af) {
        cout << i + 1 << endl;
        return;
      }
    } else if ((int)((mpv).size()) == 1) {
      if (mpv.begin()->first == 1 || (int)((mpv.begin()->second).size()) == 1) {
        cout << i + 1 << endl;
        return;
      }
    }
    long long fre = mp[a[i]];
    mp[a[i]]--;
    if ((int)((mpv[fre]).size()) == 1)
      mpv.erase(fre);
    else {
      mpv[fre].erase(a[i]);
    }
    if (fre != 1) {
      fre--;
      mpv[fre].insert(a[i]);
    }
  }
  if (f == 0) {
    cout << 1 << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
