#include <bits/stdc++.h>
using namespace std;
int a[4010];
int last[1000100];
int sol[1000100];
map<int, bool> beenx;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int maxx = 1;
  for (int i = 0; i < n; i++) {
    int x = a[i];
    if (beenx[x]) continue;
    beenx[x] = true;
    int lastx = -1;
    memset(last, -1, sizeof last);
    memset(sol, 0, sizeof sol);
    int xx = 0;
    int mx = 0, mlast = -1;
    for (int j = 0; j < n; j++) {
      if (a[j] == x) {
        xx++;
        lastx = j;
        continue;
      }
      int lst = last[a[j]];
      if (lastx != -1) {
        if (lst == -1) {
          sol[a[j]] = 1;
        } else if (lst <= lastx) {
          sol[a[j]] += 2;
        }
      } else
        sol[a[j]] = 1;
      last[a[j]] = j;
      if (sol[a[j]] > mx) {
        mx = sol[a[j]];
        mlast = last[a[j]];
      }
    }
    maxx = max(maxx, xx);
    if (lastx > mlast) mx++;
    if (mx > maxx) maxx = mx;
  }
  printf("%d\n", maxx);
  return 0;
}
