#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
const int MOD = 1000000007;
int a[5555], pr[5555], last[1000001];
int dp[4004][4004];
int pre[4004][4004];
int getp(int l, int r) {
  if (pre[l][r] != -1) return pre[l][r];
  if (r < l) return pre[l][r] = r;
  return pre[l][r] = getp(l, pr[r]);
}
int get(int l, int r) {
  if (l == 0) return 1;
  if (dp[l][r] != -1) return dp[l][r];
  int tt = getp(l, r);
  return dp[l][r] = 1 + get(tt, l);
}
int cnt[1000001];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) {
    pr[i] = last[a[i]];
    last[a[i]] = i;
    ++cnt[a[i]];
  }
  int ans = 0;
  for (int i = 1; i <= 1000000; ++i) ans = max(ans, cnt[i]);
  memset(dp, -1, sizeof(dp));
  memset(pre, -1, sizeof(pre));
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= i - 1; ++j)
      if (a[i] != a[j]) ans = max(ans, get(j, i));
  cout << ans << endl;
}
