#include <bits/stdc++.h>
using namespace std;
int dp[4010][4010];
int a[4010], ans, n;
int cal(int now, int prev);
int maxi(int a, int b) {
  if (a > b) return a;
  return b;
}
vector<int> v[1000100];
int main() {
  int i, j, k, l, test, t = 1;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    v[a[i]].push_back(i);
  }
  for (i = 1; i <= n; i++) {
    v[a[i]].push_back(n + 2);
  }
  if (n == 1) {
    printf("1\n");
    return 0;
  }
  memset(dp, -1, sizeof(dp));
  for (i = 1; i <= n; i++) {
    for (j = i + 1; j <= n; j++) {
      ans = maxi(ans, cal(j, i) + 2);
    }
  }
  printf("%d\n", ans);
  return 0;
}
int cal(int now, int prev) {
  if (dp[now][prev] != -1) return dp[now][prev];
  int ret = 0, val = a[prev];
  int k = lower_bound(v[val].begin(), v[val].end(), now + 1) - v[val].begin();
  k = v[val][k];
  if (k > n) return dp[now][prev] = 0;
  return dp[now][prev] = cal(k, now) + 1;
}
