#include <bits/stdc++.h>
using namespace std;
int arr[4005], dp[4005][4005];
map<int, int> ind;
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    ind[arr[i]] = 0;
  }
  dp[0][0] = 1;
  int ans = 1;
  for (int i = 0; i <= n; i++) dp[0][i] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      dp[i][j] = max(dp[i][j], dp[ind[arr[j]]][i] + 1);
      ans = max(ans, dp[i][j]);
    }
    ind[arr[i]] = i;
  }
  cout << ans << endl;
  return 0;
}
