#include <bits/stdc++.h>
using namespace std;
const int MOD(1000000007);
const int INF((1 << 30) - 1);
const int MAXN(4004);
const int MAXK(1000001);
int a[MAXN], b[MAXK] = {0}, p[MAXN], np = 0, d[MAXK] = {0};
bool chk[MAXK] = {0};
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (!chk[a[i]]) chk[a[i]] = 1, p[np++] = a[i];
  }
  int MAX = 0, k = 1;
  for (int i = 0; i < n; i++) {
    int j = i + 1;
    for (int j = 0; j < np; j++) d[p[j]] = 0;
    int ans = 1, tmp = 0, cnt = 1;
    while (1) {
      while (a[j] != a[i] && j < n) {
        if (b[a[j]] != k) {
          b[a[j]] = k;
          d[a[j]]++;
          tmp = max(tmp, d[a[j]]);
        }
        j++;
      }
      ans = max(ans, 2 * tmp);
      k++;
      if (j == n) break;
      ans = max(ans, 2 * tmp + 1);
      cnt++, j++;
    }
    ans = max(ans, cnt);
    MAX = max(MAX, ans);
  }
  printf("%d", MAX);
}
