#include <bits/stdc++.h>
using namespace std;
vector<int> idx[1000001];
set<pair<int, int> > mark;
int arr[4001];
int ans = 0;
int calc(int a, int b) {
  if (arr[a] == arr[b]) return 0;
  if (idx[arr[a]].size() + idx[arr[b]].size() <= ans) return 0;
  int i = 1;
  int j = 1;
  int ans = 2;
  int last = idx[arr[b]][0];
  while (true) {
    while (i < idx[arr[a]].size() && idx[arr[a]][i] < last) i++;
    if (i == idx[arr[a]].size()) break;
    last = idx[arr[a]][i];
    i++;
    ans++;
    while (j < idx[arr[b]].size() && idx[arr[b]][j] < last) j++;
    if (j == idx[arr[b]].size()) break;
    last = idx[arr[b]][j];
    j++;
    ans++;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    idx[arr[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) ans = max(ans, (int)idx[arr[i]].size());
  vector<int> u;
  for (int i = 0; i < n; i++) u.push_back(arr[i]);
  sort(u.begin(), u.end());
  u.resize(unique(u.begin(), u.end()) - u.begin());
  int first[4001];
  for (int i = 0; i < u.size(); i++) {
    for (int j = 0; j < n; j++) {
      if (arr[j] == u[i]) {
        first[i] = j;
        break;
      }
    }
  }
  int x, y;
  for (int i = 0; i < u.size(); i++) {
    for (int j = i + 1; j < u.size(); j++) {
      x = first[i];
      y = first[j];
      ans = max(ans, calc(min(x, y), max(x, y)));
    }
  }
  cout << ans << endl;
  return 0;
}
