#include <bits/stdc++.h>
using namespace std;
int n, a[4444], d[4444][4444], ans, w[1111111], cc, cur, x, y, last[4444];
int main() {
  scanf("%d", &n);
  if (n < 3) {
    printf("%d", n);
    return 0;
  }
  cc = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    if (!w[a[i]]) w[a[i]] = ++cc;
    a[i] = w[a[i]];
  }
  memset(d, 0, sizeof(d));
  memset(last, 0, sizeof(last));
  d[a[n - 1]][a[n]] = (d[a[n]][a[n]] = d[a[n - 1]][a[n - 1]] = 1) + 1;
  for (int i = n - 2; i > 0; --i)
    for (int j = i + 1; j <= n; ++j) {
      if (last[a[j]] == i) continue;
      if (!d[y = a[j]][x = a[i]])
        cur = 2;
      else
        cur = d[y][x] + 1;
      d[x][y] = max(d[x][y], cur);
      last[a[j]] = i;
    }
  ans = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = i; j <= n; ++j) ans = max(ans, d[i][j]);
  printf("%d", ans);
  return 0;
}
