#include <bits/stdc++.h>
using namespace std;
int n, a[5555], f[4444][4444], b[4444], q[1000111], ans = 0, s[1000111];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i <= 1000000; i++) q[i] = n;
  for (int i = n - 1; i >= 0; i--) b[i] = q[a[i]], q[a[i]] = i, s[a[i]]++;
  for (int i = 0; i <= 1000000; i++) ans = max(ans, s[i]);
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      if (a[i] != a[j]) f[i][j] = 2;
  for (int i = 0; i < n; i++) {
    int x = b[i];
    for (int j = i + 1; j < n; j++)
      if (a[i] != a[j]) {
        while (x < j) x = b[x];
        if (x != n) f[j][x] = max(f[j][x], f[i][j] + 1);
      }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) ans = max(ans, f[i][j]);
  cout << (ans) << endl;
  return 0;
}
