#include <bits/stdc++.h>
using namespace std;
int n, arr[100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    arr[i] = k;
    if (mp.find(k) == mp.end()) {
      mp[k] = 1;
    } else {
      mp[k]++;
    }
  }
  map<int, int> mp2;
  for (auto x : mp) {
    if (mp2.find(x.second) == mp2.end()) {
      mp2[x.second] = 1;
    } else {
      mp2[x.second]++;
    }
  }
  int pt = n;
  while (mp2.size() > 2) {
    int g = arr[pt];
    int banyak = mp[g];
    mp[g]--;
    if (mp2[banyak] == 1) {
      mp2.erase(banyak);
    } else {
      mp2[banyak]--;
    }
    if (banyak != 1) {
      if (mp2.find(banyak - 1) == mp2.end()) {
        mp2[banyak - 1] = 1;
      } else {
        mp2[banyak - 1]++;
      }
    }
    pt--;
  }
  for (int ptr = pt; ptr >= 1; ptr--) {
    if (mp2.size() == 2) {
      int a = -1, b;
      for (auto x : mp2) {
        if (a == -1) {
          a = x.first;
        } else {
          b = x.first;
        }
      }
      if (a - 1 == b && mp2[a] == 1) {
        cout << ptr << '\n';
        break;
      } else if (b - 1 == a && mp2[b] == 1) {
        cout << ptr << '\n';
        break;
      } else {
        if (a == 1 && mp2[a] == 1) {
          cout << ptr << '\n';
          break;
        } else if (b == 1 && mp2[b] == 1) {
          cout << ptr << '\n';
          break;
        }
      }
    } else if (mp2.size() == 1) {
      int a;
      for (auto x : mp2) {
        a = x.first;
      }
      if (mp2[a] == 1) {
        cout << ptr << '\n';
        break;
      } else if (a == 1) {
        cout << ptr << '\n';
        break;
      }
    }
    int g = arr[ptr];
    int banyak = mp[g];
    mp[g]--;
    if (mp2[banyak] == 1) {
      mp2.erase(banyak);
    } else {
      mp2[banyak]--;
    }
    if (banyak != 1) {
      if (mp2.find(banyak - 1) == mp2.end()) {
        mp2[banyak - 1] = 1;
      } else {
        mp2[banyak - 1]++;
      }
    }
  }
  return 0;
}
