#include <bits/stdc++.h>
using namespace std;
int n;
int a[4002];
int dp[4002][4002];
int doDp() {
  int ret = -1;
  int prev = 0;
  for (int i = 1; i <= n; ++i) {
    prev = 0;
    for (int j = 0; j < i; ++j) {
      dp[j][i] = dp[prev][j] + 1;
      if (a[i] == a[j]) prev = j;
      ret = max(ret, dp[j][i]);
    }
  }
  return ret;
}
int main() {
  cin >> n;
  memset(dp, 0, sizeof dp);
  memset(a, 0, sizeof a);
  for (int i = 1; i <= n; ++i) cin >> a[i];
  cout << doDp();
  return 0;
}
