#include <bits/stdc++.h>
using namespace std;
int m[1000001];
const int size = 4001;
int a[size], dp[size][size];
int main() {
  int n;
  cin >> n;
  int k = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (m[a[i]] == 0) m[a[i]] = k++;
  }
  int ans = 1;
  for (int i = n; i >= 1; i--) {
    int c = m[a[i]];
    for (int j = 0; j < k; j++) {
      dp[c][j] = max(dp[c][j], dp[j][c] + 1);
      ans = max(ans, dp[c][j]);
    }
  }
  cout << ans;
  return 0;
}
