#include <bits/stdc++.h>
using namespace std;
int n, a[4000], num[1000000], i, pr[4000][4000], j, d[4000][4000], san, ans = 1;
vector<int> last(4000, -1);
int main() {
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (num[a[i] - 1])
      a[i] = num[a[i] - 1];
    else {
      num[a[i] - 1] = ++san;
      a[i] = san;
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < 4000; j++) pr[j][i] = last[j];
    last[a[i] - 1] = i;
  }
  for (i = 1; i < n; i++)
    for (j = 0; j < i; j++) {
      if (pr[a[i] - 1][j] != -1)
        d[i][j] = d[j][pr[a[i] - 1][j]] + 1;
      else
        d[i][j] = 2;
      if (d[i][j] > ans) ans = d[i][j];
    }
  cout << ans;
  return 0;
}
