#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000001;
int a[4000];
int dp[maxn] = {0};
int fr[maxn] = {0};
int vs[maxn] = {0};
int ls[maxn] = {0};
int cnt[maxn] = {0};
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    cnt[a[i]]++;
  }
  int maxcnt = 0;
  for (int i = 0; i < maxn; i++) {
    maxcnt = max(maxcnt, cnt[i]);
  }
  int t = 0;
  for (int i = 0; i < n; i++) {
    int v = a[i];
    int prev = -1;
    t++;
    int newcnt = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] == v) {
        prev = j;
      } else {
        if (vs[a[j]] == t) {
          if (fr[a[j]] < prev) {
            dp[a[j]] += 2;
            fr[a[j]] = j;
            vs[a[j]] = t;
          }
        } else {
          if (prev == -1) {
            dp[a[j]] = 1;
          } else {
            dp[a[j]] = 2;
          }
          fr[a[j]] = j;
          vs[a[j]] = t;
          ls[newcnt++] = a[j];
        }
      }
    }
    for (int j = 0; j < newcnt; j++) {
      if (fr[ls[j]] < prev && prev != -1) {
        dp[ls[j]]++;
        fr[ls[j]] = prev;
      }
      maxcnt = max(maxcnt, dp[ls[j]]);
    }
  }
  printf("%d", maxcnt);
}
