#include <bits/stdc++.h>
using std::max;
std::map<int, int> map;
int ary[4000], N, DP[4000][4001];
int main() {
  int i, j, idn, n, m, ans = 1;
  scanf("%d", &N);
  for (j = i = 0; i < N; i++) {
    scanf("%d", ary + i);
    if (!map[ary[i]]) map[ary[i]] = ++j;
    ary[i] = map[ary[i]];
  }
  idn = j;
  for (i = 1; i < N; i++)
    for (j = 0; j < i; j++) {
      n = ary[i];
      m = ary[j];
      if (DP[j][n] == 0) DP[j][n] = 1;
      DP[i][m] = max(DP[i][m], DP[j][n] + 1);
      ans = max(ans, DP[i][m]);
    }
  printf("%d\n", ans);
}
