#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4010, MAXM = 1000 * 1001;
int a[MAXN] = {}, dp[MAXN][MAXN] = {}, b[MAXM] = {};
set<int> s[MAXM];
set<int>::iterator it;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, x = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x = max(x, ++b[a[i]]);
    s[a[i]].insert(i);
  }
  for (int i = 0; i < n; i++) dp[i][i] = 1;
  int q;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      it = s[a[j]].lower_bound(i);
      if (it == s[a[j]].begin())
        dp[i][j] = 2;
      else {
        it--;
        q = *it;
        dp[i][j] = dp[q][i] + 1;
      }
      x = max(dp[i][j], x);
    }
  }
  cout << x;
}
