#include <bits/stdc++.h>
using namespace std;
const int MAX = 4010;
int arr[MAX];
int temp[MAX];
int memo[MAX][MAX];
vector<int> idx[MAX];
map<int, int> mp;
int n;
int solve(int i, int prev) {
  int &ret = memo[i][prev];
  if (~ret) return ret;
  ret = -1e9;
  int next = arr[prev];
  auto it = upper_bound(idx[next].begin(), idx[next].end(), i);
  if (it != idx[next].end()) {
    return ret = max(ret, solve(*it, i) + 1);
  } else
    return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &arr[i]), temp[i] = arr[i];
  if (n <= 2) {
    printf("%d\n", n);
    return 0;
  }
  sort(temp, temp + n);
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (mp.count(temp[i]) == 0) mp[temp[i]] = ++cnt;
  }
  for (int i = 0; i < n; ++i) arr[i] = mp[arr[i]];
  for (int i = 0; i < n; ++i) idx[arr[i]].push_back(i);
  memset(memo, -1, sizeof(memo));
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      ans = max(ans, solve(i, j));
    }
  }
  printf("%d\n", ans + 2);
}
