#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> v(N), w(N);
  for (int i = 0; i < (N); ++i) {
    cin >> v[i];
    w[i] = v[i];
  }
  sort(w.begin(), w.end());
  w.erase(unique(w.begin(), w.end()), w.end());
  int K = w.size();
  vector<vector<int> > t(K, vector<int>(K));
  for (int i = 0; i < (N); ++i) {
    int idx = distance(w.begin(), lower_bound(w.begin(), w.end(), v[i]));
    for (int j = 0; j < (K); ++j)
      if (t[idx][j] + 1 > t[j][idx]) t[j][idx] = t[idx][j] + 1;
  }
  int mx = 0;
  for (int i = 0; i < (K); ++i)
    for (int j = 0; j < (K); ++j)
      if (t[i][j] > mx) mx = t[i][j];
  cout << mx << endl;
  return 0;
}
