#include <bits/stdc++.h>
using namespace std;
vector<int> pos[1000010];
vector<int> b;
int dp[4010][4010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int temp;
    scanf("%d", &temp);
    b.push_back(temp);
    pos[temp].push_back(i);
  }
  memset(dp, 0, sizeof(dp));
  int ans = 1;
  for (int last = 0; last < n; last++) {
    for (int second_last = 0; second_last < last; second_last++) {
      if (b[last] == b[second_last]) {
        vector<int>::iterator it =
            lower_bound(pos[b[last]].begin(), pos[b[last]].end(), second_last);
        if (it == pos[b[last]].begin()) {
          dp[last][second_last] = 2;
        } else {
          it--;
          dp[last][second_last] = 1 + dp[second_last][*it];
        }
      } else {
        vector<int>::iterator it =
            upper_bound(pos[b[last]].begin(), pos[b[last]].end(), second_last);
        if (it == pos[b[last]].begin()) {
          dp[last][second_last] = 2;
        } else {
          it--;
          dp[last][second_last] = 1 + dp[second_last][*it];
        }
      }
      ans = max(ans, dp[last][second_last]);
    }
  }
  cout << ans;
  return 0;
}
