#include <bits/stdc++.h>
using namespace std;
int n, a[4444], cnt[4444];
map<int, int> id;
vector<int> pos[4444];
int dp[4444];
int vis[4444];
int prev1(int el, int idx) {
  vector<int>::iterator it = lower_bound(pos[el].begin(), pos[el].end(), idx);
  if (it == pos[el].begin())
    return -1;
  else {
    it--;
    return (*it);
  }
}
bool pres(int el, int idx1, int idx2) {
  vector<int>::iterator i1 = lower_bound(pos[el].begin(), pos[el].end(), idx1);
  vector<int>::iterator i2 = lower_bound(pos[el].begin(), pos[el].end(), idx2);
  int count = 3;
  for (; i1 != i2 && count--; i1++)
    if ((*i1) > idx1 && (*i1) < idx2)
      return true;
    else if ((*i1) > idx2)
      return false;
  return false;
}
int main() {
  scanf("%d", &n);
  int idc = 0, fans = -1;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (id.find(x) != id.end()) {
      a[i] = id[x];
    } else {
      id[x] = ++idc;
      a[i] = idc;
    }
    cnt[a[i]]++;
    pos[a[i]].push_back(i);
    fans = max(fans, cnt[a[i]]);
  }
  for (int i = 0; i < idc; i++) pos[i].push_back(4444);
  if (n <= 2) {
    printf("%d\n", n);
    return 0;
  }
  fans = max(fans, 2);
  for (int i = 0; i < n; i++) {
    int p = a[i];
    if (vis[p]) continue;
    vis[p] = 1;
    memset(dp, 0, sizeof dp);
    int cans = -1;
    for (int j = i + 1; j < n; j++) {
      if (a[j] == p) {
      } else {
        int len = (int)pos[p].size() - 2;
        int pidx = prev1(a[j], j);
        if (pidx < i) {
          dp[j] = 2;
          if (pos[p][len] > j) {
            cans = max(cans, dp[j] + 1);
          }
        } else {
          if (pres(p, pidx, j)) {
            dp[j] = max(dp[j], dp[pidx] + 2);
            if (pos[p][len] > j) {
              cans = max(cans, dp[j] + 1);
            }
          } else {
            dp[j] = dp[pidx];
          }
        }
      }
      cans = max(cans, dp[j]);
    }
    fans = max(fans, cans);
  }
  printf("%d\n", fans);
  return 0;
}
