#include <bits/stdc++.h>
using namespace std;
int use[1000005], cnt[1000005], a[4005], Next[1000005], last[1000005];
pair<int, int> b[4005];
int n, ans = 1, num;
void UP(int l, int r) {
  ++num;
  for (int i = l; i <= r; i++) {
    if (use[a[i]] == num) continue;
    use[a[i]] = num;
    cnt[a[i]]++;
  }
}
void Solve(int pos) {
  int W = 0;
  int S = pos;
  while (pos != n + 1) {
    UP(pos + 1, Next[pos] - 1);
    pos = Next[pos];
    W++;
  }
  ans = max(ans, W);
  for (int i = 1; i <= n; i++)
    ans = max(ans, cnt[a[i]] * 2 + (last[a[S]] > last[a[i]]));
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    last[a[i]] = i;
    b[i].first = a[i];
    b[i].second = i;
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; i++) Next[i] = n + 1;
  for (int i = 1; i < n; i++)
    if (b[i].first == b[i + 1].first) Next[b[i].second] = b[i + 1].second;
  for (int i = 1; i <= n; i++) {
    for (int i = 1; i <= n; i++) cnt[a[i]] = 0;
    Solve(i);
  }
  cout << ans;
}
