#include <bits/stdc++.h>
using namespace std;
int N;
int B[4000];
int nxt[4000][4000];
vector<int> v;
int dp[4000][4000];
int cc[4000];
int main() {
  scanf("%d", &N);
  for (int(i) = 0; (i) < (N); (i)++) scanf("%d", &B[i]), v.push_back(B[i]);
  sort((v).begin(), (v).end());
  (v).erase(unique((v).begin(), (v).end()), (v).end());
  for (int(i) = 0; (i) < (N); (i)++)
    B[i] = lower_bound((v).begin(), (v).end(), (B[i])) - (v).begin(),
    cc[B[i]]++;
  memset((nxt), (-1), sizeof(nxt));
  for (int(i) = 0; (i) < (N); (i)++) nxt[B[i]][i] = i;
  for (int(i) = 0; (i) < (v.size()); (i)++)
    for (int j = N - 2; j >= 0; j--)
      if (nxt[i][j] == -1) nxt[i][j] = nxt[i][j + 1];
  int res = 1;
  res = max(res, *max_element(cc, cc + 4000));
  for (int(i) = 0; (i) < (N); (i)++)
    for (int(j) = (i + 1); (j) < (N); (j)++)
      if (B[i] != B[j]) dp[i][j] = 2;
  for (int(i) = 0; (i) < (N); (i)++)
    for (int(j) = 0; (j) < (N); (j)++)
      if (dp[i][j]) {
        res = max(res, dp[i][j]);
        if (i < j && nxt[B[i]][j] != -1)
          dp[nxt[B[i]][j]][j] = max(dp[nxt[B[i]][j]][j], dp[i][j] + 1);
        if (i > j && nxt[B[j]][i] != -1)
          dp[i][nxt[B[j]][i]] = max(dp[i][nxt[B[j]][i]], dp[i][j] + 1);
      }
  printf("%d\n", res);
}
