#include <bits/stdc++.h>
using namespace std;
int n;
int dat[4096];
int state[1048576];
int vstate[1048576], v;
int upstate[1048576];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &dat[i]);
  }
  int ans = 1;
  for (int i = 0; i < n; i++) {
    int start = dat[i];
    ++v;
    int up = 1;
    for (int j = i + 1; j < n; j++) {
      int val = dat[j];
      if (dat[j] == dat[i]) {
        up++;
        continue;
      }
      if (vstate[val] != v) {
        state[val] = 2;
        vstate[val] = v;
        upstate[val] = up;
      } else {
        if (upstate[val] != up) {
          state[val] += 2;
          upstate[val] = up;
        }
      }
      ans = max(ans, state[val]);
    }
    for (int j = i + 1; j < n; j++) {
      int val = dat[j];
      if (val == dat[i]) continue;
      if (upstate[val] != up) {
        ans = max(ans, state[val] + 1);
      }
    }
    ans = max(ans, up);
  }
  printf("%d\n", ans);
  return 0;
}
