#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
long long a[maxn];
long long dp[4005][4005];
long long n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0, pv = 0; j < i; j++) {
      dp[i][j] = dp[j][pv] + 1;
      if (a[i] == a[j]) pv = j;
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans;
  return 0;
}
