#include <bits/stdc++.h>
using namespace std;
int n;
int arr[4004];
int mem[4004][4004][2], Next[4004];
bool visited[1000001];
int solve(int i, int other, bool e5tart) {
  if (i == n) return 0;
  if (mem[i][other][e5tart] != -1) return mem[i][other][e5tart];
  int ret, nextt;
  int u = Next[other];
  while (1) {
    if (u > i) {
      nextt = u;
      break;
    }
    u = Next[u];
  }
  ret = solve(nextt, i, 1) + 1;
  if (!e5tart) {
    ret = max(ret, solve(i + 1, other, 0));
  }
  return mem[i][other][e5tart] = ret;
}
int main() {
  memset(mem, -1, sizeof mem);
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) {
    bool b = 0;
    for (int j = i + 1; j < n; j++) {
      if (arr[j] == arr[i]) {
        Next[i] = j;
        b = 1;
        break;
      }
    }
    if (!b) Next[i] = n;
  }
  for (int i = 0; i < n; i++) {
    if (visited[arr[i]]) continue;
    visited[arr[i]] = 1;
    ans = max(ans, solve(i + 1, i, 0) + 1);
  }
  cout << ans;
  return 0;
}
