#include <bits/stdc++.h>
using namespace std;
int arr[1000000], is[4001][4001], memo[4001][4001];
map<int, set<int> > mp;
int recursive(int s1, int s2) {
  if (is[s1][s2]) return memo[s1][s2];
  is[s1][s2] = true;
  if (mp[arr[s1]].upper_bound(s2) != mp[arr[s1]].end()) {
    int yo = *mp[arr[s1]].upper_bound(s2);
    return memo[s1][s2] = recursive(s2, yo) + 1;
  } else
    return memo[s1][s2] = 0;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mp[arr[i]].insert(i);
  }
  int maxi = 1;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      maxi = max(maxi, recursive(i, j) + 2);
    }
  }
  cout << maxi;
  return 0;
}
