#include <bits/stdc++.h>
using namespace std;
int f[4005][4005];
int a[4005];
int n, nex[4005];
map<int, int> mm;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = n; i >= 1; i--) {
    if (mm[a[i]] == 0)
      nex[i] = n + 1;
    else
      nex[i] = mm[a[i]];
    mm[a[i]] = i;
  }
  int res = -1;
  for (int i = 1; i <= n; i++) {
    int t = i;
    for (int j = i + 1; j <= n; j++) {
      while (t != n + 1 && t <= j) t = nex[t];
      f[j][t] = max(f[j][t], f[i][j] + 1);
      res = max(res, f[i][j]);
    }
  }
  cout << res + 2;
  return 0;
}
