#include <bits/stdc++.h>
int n, a[4000 + 1], *p[4000 + 1], cnt, f[4000 + 1][2], d[4000 + 1], ans = 0;
inline bool cmp(int *a, int *b) { return *a < *b; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i), p[i] = &a[i];
  std::sort(p + 1, p + n + 1, cmp);
  cnt = 0;
  for (int i = 1, j; i <= n; i = j) {
    for (j = i + 1; j <= n && *p[i] == *p[j]; ++j)
      ;
    ++cnt, ans = std::max(ans, j - i);
    for (int k = i; k < j; ++k) *p[k] = cnt;
  }
  for (int q = 1; q < cnt; ++q) {
    for (int i = 1; i <= cnt; ++i) f[i][0] = f[i][1] = 0;
    for (int i = 1; i <= n; ++i) {
      d[i] = 0;
      if (a[i] > q) d[i] = std::max(f[a[i] - q][0] + 1, d[i]);
      if (a[i] + q <= cnt) d[i] = std::max(f[a[i] + q][1] + 1, d[i]);
      if (a[i] > q) f[a[i]][1] = std::max(f[a[i] - q][0] + 1, f[a[i]][1]);
      if (a[i] + q <= cnt)
        f[a[i]][0] = std::max(f[a[i] + q][1] + 1, f[a[i]][0]);
    }
    for (int i = 1; i <= n; ++i) ans = std::max(ans, d[i]);
  }
  printf("%d\n", ans);
  return 0;
}
