#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = acos(-1);
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T>
T abs(T x) {
  return x < 0 ? -x : x;
}
template <class T>
vector<T> ReadVector(int n) {
  vector<T> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  return a;
}
template <class T>
void WriteVector(const vector<T>& a) {
  for (int i = 0; i < ((int)(a).size()); ++i) {
    cout << a[i] << (i + 1 < ((int)(a).size()) ? ' ' : '\n');
  }
}
void Solve() {
  int n;
  cin >> n;
  auto a = ReadVector<int>(n);
  for (int i = 0; i < n; ++i) {
    --a[i];
  }
  int max_a = 0;
  for (int i = 0; i < n; ++i) {
    max_a = max(max_a, a[i]);
  }
  vector<int> cnt(max_a + 1);
  for (int i = 0; i < n; ++i) {
    ++cnt[a[i]];
  }
  int x = 0;
  bool key = false;
  for (int i = 1; i <= max_a; ++i) {
    if (cnt[i] > cnt[x]) {
      key = false;
      x = i;
    } else if (cnt[i] == cnt[x]) {
      key = true;
    }
  }
  if (key) {
    cout << n << "\n";
    return;
  }
  int res = 0;
  for (int y = 0; y <= max_a; ++y) {
    if (x == y || cnt[y] == 0) {
      continue;
    }
    vector<int> b(n + 1);
    for (int i = 0; i < n; ++i) {
      if (a[i] == x) {
        b[i + 1] = b[i] + 1;
      } else if (a[i] == y) {
        b[i + 1] = b[i] - 1;
      } else {
        b[i + 1] = b[i];
      }
    }
    int max_b = b[0];
    int min_b = b[0];
    for (int i = 1; i <= n; ++i) {
      max_b = max(max_b, b[i]);
      min_b = min(min_b, b[i]);
    }
    vector<int> left(max_b - min_b + 1, -1);
    vector<int> right(max_b - min_b + 1);
    for (int i = 0; i <= n; ++i) {
      if (left[b[i] - min_b] == -1) {
        left[b[i] - min_b] = i;
      }
      right[b[i] - min_b] = i;
    }
    for (int i = 0; i <= max_b - min_b; ++i) {
      res = max(res, right[i] - left[i]);
    }
  }
  cout << res << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  std::cin.tie(nullptr);
  int tc = 1;
  for (int ti = 0; ti < tc; ++ti) {
    Solve();
  }
  return 0;
}
