#include <bits/stdc++.h>
int N, a[4001], dp[4001][4001], last[1000001], prev[4001], ans = 1;
inline int max(int a, int b) { return a > b ? a : b; }
int main() {
  int i, j, t;
  scanf("%d", &N);
  for (i = 1; i <= N; ++i) {
    scanf("%d", a + i);
    t = prev[i] = last[a[i]];
    last[a[i]] = i;
    dp[i][0] = 1;
    for (j = i - 1; j > 0; --j) {
      dp[i][j] = 2;
      if (t >= j) t = prev[t];
      if (t != 0) dp[i][j] = dp[j][t] + 1;
      if (dp[i][j] > ans) ans = dp[i][j];
    }
  }
  printf("%d", ans);
  return 0;
}
