#include <bits/stdc++.h>
using namespace std;
int pre[4005][2], sum[4005][4005], first[4005];
int num[1000005], ori[1000005], nn, n;
map<int, int> mm;
int main() {
  scanf("%d", &n);
  memset(first, -1, sizeof first);
  for (int i = 0; i < n; i++) {
    scanf("%d", &num[i]);
    ori[i] = num[i];
  }
  sort(num, num + n);
  nn = unique(num, num + n) - num;
  for (int i = 0; i < n; i++) {
    int idx = lower_bound(num, num + nn, ori[i]) - num, idx2;
    mm.clear();
    sum[idx][idx]++;
    for (int j = i - 1; j >= 0; j--) {
      if (ori[j] == ori[i]) break;
      idx2 = lower_bound(num, num + nn, ori[j]) - num;
      if (mm[ori[j]])
        continue;
      else {
        mm[ori[j]] = 1;
        if (sum[idx][idx2] == 0 && first[idx] != -1)
          sum[idx][idx2] += 3;
        else
          sum[idx][idx2] += 2;
      }
    }
    first[idx] = i;
  }
  int ans = 1;
  for (int i = 0; i < nn; i++)
    for (int j = 0; j < nn; j++) ans = max(ans, sum[i][j]);
  printf("%d\n", ans);
  return 0;
}
