#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <typename T>
void read(T &a) {
  T x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  a = x * f;
}
template <typename T>
void print(T x) {
  if (!x) return;
  if (x < 0) putchar('-'), x = -x;
  print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T, typename... Args>
void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
long long n, a[200005 << 1], res = 0, las[200005 << 1];
map<long long, long long> mp;
signed main() {
  read(n);
  for (long long i = 1; i <= n; i++) read(a[i]), mp[a[i]]++;
  long long maxid, maxx = -1;
  for (map<long long, long long>::iterator it = mp.begin(); it != mp.end();
       it++) {
    if (maxx < it->second) {
      maxx = it->second;
      maxid = it->first;
    }
  }
  for (map<long long, long long>::iterator it = mp.begin(); it != mp.end();
       it++) {
    if (it->first == maxid) continue;
    long long sum = n;
    memset(las, -1, sizeof(las));
    las[n] = 0;
    for (long long i = 1; i <= n; i++) {
      if (a[i] == it->first) sum--;
      if (a[i] == maxid) sum++;
      if (las[sum] != -1)
        res = max(res, i - las[sum]);
      else
        las[sum] = i;
    }
  }
  printf("%lld\n", res);
  return 0;
}
