#include <bits/stdc++.h>
using namespace std;
int a[4005], f[4005][4005];
int pre[1000005], now[1000005], cnt[1000005];
int main() {
  int i, j, n, p, ans = 1;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    pre[i] = now[a[i]];
    now[a[i]] = i;
    ans = max(ans, ++cnt[a[i]]);
  }
  for (i = 1; i <= n; ++i)
    for (j = i + 1; j <= n; ++j)
      if (a[i] != a[j]) {
        p = pre[j];
        if (i < p)
          f[i][j] = f[i][p];
        else
          f[i][j] = f[p][i] + 1;
        if (f[i][j] < 2) f[i][j] = 2;
        ans = max(ans, f[i][j]);
      }
  printf("%d\n", ans);
  return 0;
  ;
}
