#include <bits/stdc++.h>
using namespace std;
const int N = 4005;
int dp[N][N];
int a[N];
int main() {
  int n;
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    memset(dp, 0, sizeof(dp));
    map<int, int> last;
    int res = 1;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        dp[i][j] = 2;
        if (last.count(a[j]) != 0) {
          int id = last[a[j]];
          dp[i][j] = max(dp[i][j], dp[id][i] + 1);
        }
        res = max(res, dp[i][j]);
      }
      last[a[i]] = i;
    }
    cout << res << endl;
  }
  return 0;
}
