#include <bits/stdc++.h>
using namespace std;
int N, A[4002];
int L[4002][4002];
int last[1000002];
int result;
int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i) cin >> A[i];
  for (int i = 2; i <= N; ++i) {
    for (int j = 1; j < i; ++j) {
      L[i][j] = 2;
      if (last[A[i]] != 0) L[i][j] = max(L[i][j], L[j][last[A[i]]] + 1);
      result = max(result, L[i][j]);
      last[A[j]] = j;
    }
    for (int j = 1; j <= i; ++j) last[A[j]] = 0;
  }
  if (N == 1) result = 1;
  cout << result << '\n';
}
