#include <bits/stdc++.h>
using namespace std;
int n, i, j, a[4005], dp[4005][4005], num[4005];
vector<int> tmp;
int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    tmp.push_back(a[i]);
  }
  sort(tmp.begin(), tmp.end());
  tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin());
  for (i = 0; i < n; i++)
    a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin();
  int res = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) num[j] = dp[a[j]][a[i]] + 1;
    for (j = 0; j < n; j++) {
      dp[a[i]][a[j]] = max(dp[a[i]][a[j]], num[j]);
      res = max(res, dp[a[i]][a[j]]);
    }
  }
  printf("%d\n", res);
}
