#include <bits/stdc++.h>
using namespace std;
int n;
struct xs {
  int num;
  int wei;
};
int mx;
xs s[5001];
bool com(xs d, xs e) {
  return d.num < e.num || (d.num == e.num && d.wei < e.wei);
}
int main(int argc, char *argv[]) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i].num;
    s[i].wei = i;
  }
  bool pan = 0;
  sort(s, s + n, com);
  for (int i = 0; i < n; i++) {
    int start = i;
    while (s[i].num == s[i + 1].num) i++;
    mx = max(i - start + 1, mx);
    if (pan) cout << s[i].num << " :  ";
    for (int j = 0; j < start; j++) {
      int p = j;
      while (s[p].num == s[p + 1].num) p++;
      int sum = s[start].wei < s[j].wei;
      int o = j;
      int last = -1;
      int q = start;
      while (1) {
        while (s[o].wei <= last && o <= p) o++;
        if (o <= p) {
          sum++;
          last = s[o].wei;
        } else
          break;
        while (s[q].wei <= last && q <= i) q++;
        if (q <= i) {
          sum++;
          last = s[q].wei;
        } else
          break;
      }
      mx = max(mx, sum);
      j = p;
    }
  }
  if (mx == 0) mx = n;
  cout << mx << endl;
  return 0;
}
