#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  scanf("%d", &N);
  if (N == 1) {
    printf("1\n");
    return 0;
  }
  static int A[4000];
  for (int i = 0; i < N; i++) scanf("%d", &A[i]);
  static int First[1000001];
  for (int i = 0; i <= 1000000; i++) First[i] = -1;
  static int DP[4000][4000];
  int Ans = 0;
  for (int i = N - 1; i >= 0; i--) {
    for (int j = 0; j < i; j++) {
      DP[j][i] = 2;
      if (First[A[j]] != -1) DP[j][i] = DP[i][First[A[j]]] + 1;
      Ans = max(Ans, DP[j][i]);
    }
    First[A[i]] = i;
  }
  printf("%d\n", Ans);
  return 0;
}
