#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15 + 9;
long long dp[4009][4009];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  long long vet[n + 2];
  long long vet2[n + 2];
  for (long long i = 0; i < (n); ++i) {
    cin >> vet[i];
    vet2[i] = vet[i];
  }
  int cont2 = 1;
  map<int, int> cont;
  sort(vet2, vet2 + n);
  for (long long i = 0; i < (n); ++i) {
    if (!cont.count(vet2[i])) cont[vet2[i]] = cont2++;
  }
  for (long long i = 0; i < (n); ++i) {
    vet[i] = cont[vet[i]];
  }
  long long ans = -1;
  for (long long i = 0; i < (n); ++i) {
    bool jamevi = false;
    for (long long j = i - 1; j >= 0; j--) {
      if (vet[i] == vet[j] && jamevi) continue;
      dp[vet[i]][vet[j]] = max(dp[vet[i]][vet[j]], dp[vet[j]][vet[i]] + 1);
      if (vet[i] == vet[j]) jamevi = true;
    }
  }
  for (long long i = 0; i < (4009); ++i) {
    for (long long j = 0; j < (4009); ++j) {
      ans = max(ans, dp[i][j] + 1);
    }
  }
  cout << ans << '\n';
  return 0;
}
