#include <bits/stdc++.h>
const int N = 2e5 + 10;
const int mod = 998244353;
const double PI = acos(-1.0);
const double eps = 1e-8;
using namespace std;
inline int read() {
  int num = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = (num << 3) + (num << 1) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -num : num;
}
inline int lowbit(int x) { return x & -x; }
int n, a[N], cnt[N], pos[N << 1];
int solve() {
  cin >> n;
  int mx = 0, mxcnt = 0, mxnum;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    ++cnt[a[i]];
    if (cnt[a[i]] > mx)
      mx = cnt[a[i]], mxcnt = 1, mxnum = a[i];
    else if (cnt[a[i]] == mx)
      ++mxcnt;
  }
  if (mxcnt > 1) return n;
  if (cnt[mxnum] == n) return 0;
  int ans = 0;
  for (int x = 1; x <= n; ++x) {
    if (cnt[x] == 0 || x == mxnum) continue;
    int presum = 0;
    for (int i = 0; i <= n + n + 2; i += 2) pos[i] = pos[i + 1] = 0;
    for (int i = 1; i <= n; ++i) {
      if (a[i] == mxnum)
        ++presum;
      else if (a[i] == x)
        --presum;
      int temp = presum;
      if (temp < 0) temp = -temp + n;
      if (temp == 0)
        ans = max(ans, i);
      else if (pos[temp])
        ans = max(ans, i - pos[temp]);
      if (pos[temp] == 0) pos[temp] = i;
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cout << solve() << endl;
  return 0;
}
