#include <bits/stdc++.h>
using namespace std;
const int N = 4444;
const long long INF = 1e9 + 19;
int dp[N][N];
int dp1[N];
int a[N];
int n;
void read() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
}
void solve() {
  vector<int> xx;
  for (int i = 0; i < n; i++) xx.push_back(a[i]);
  sort(xx.begin(), xx.end());
  xx.resize(unique(xx.begin(), xx.end()) - xx.begin());
  for (int i = 0; i < n; i++)
    a[i] = lower_bound(xx.begin(), xx.end(), a[i]) - xx.begin();
  int m = xx.size();
  for (int i = 0; i < n; i++) {
    memset(dp1, 0, sizeof(dp1));
    int x = a[i];
    for (int j = 0; j < i; j++) {
      int y = a[j];
      dp1[y] = max(2, dp[y][x] + 1);
    }
    for (int j = 0; j < m; j++) dp[x][j] = max(dp[x][j], dp1[j]);
  }
  int answer = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) answer = max(answer, dp[i][j]);
  printf("%d\n", answer);
}
void stress() {}
int main() {
  if (1) {
    int k = 1;
    for (int tt = 0; tt < k; tt++) {
      read();
      solve();
    }
  } else {
    stress();
  }
  return 0;
}
