#include <bits/stdc++.h>
using namespace std;
struct node {
  int num, site;
} a[4100];
int num[1010000];
bool cmp1(node p1, node p2) {
  if (p1.num != p2.num)
    return p1.num < p2.num;
  else
    return p1.site < p2.site;
}
bool cmp2(node p1, node p2) { return p1.site < p2.site; }
int dp[4100][4100];
int main() {
  int n, ans = 1, i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i].num);
    a[i].site = i;
  }
  sort(a + 1, a + 1 + n, cmp1);
  num[a[1].num] = 1;
  for (i = 2; i <= n; i++) {
    if (a[i].num == a[i - 1].num)
      continue;
    else
      num[a[i].num] = num[a[i - 1].num] + 1;
  }
  sort(a + 1, a + 1 + n, cmp2);
  memset(dp, 0, sizeof(dp));
  for (i = 1; i <= n; i++)
    for (j = 1; j < i; j++) {
      dp[i][num[a[j].num]] = 2;
      ans = 2;
    }
  for (i = 2; i <= n; i++)
    for (j = 1; j < i; j++) {
      dp[i][num[a[j].num]] =
          max(dp[i][num[a[j].num]], dp[j][num[a[i].num]] + 1);
      ans = max(ans, dp[i][num[a[j].num]]);
    }
  printf("%d\n", ans);
  return 0;
}
