#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const double PI = acos(-1.0);
const int INF = 1000000000;
const int MOD = 1000000009;
const int M = INF;
int main() {
  double TIME_START = clock();
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<int> tmp = a;
  sort(a.begin(), a.end());
  vector<int>::iterator ne = unique(a.begin(), a.end());
  map<int, int> id;
  int i = 0;
  for (vector<int>::iterator it = a.begin(); it != ne; ++it, ++i) id[*it] = i;
  a = tmp;
  for (int i = 0; i < n; ++i) {
    a[i] = id[a[i]];
  }
  int m = id.size();
  vector<vector<int> > *fi, *se;
  vector<vector<int> > m1(m, vector<int>(m, 0));
  fi = &m1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int at = a[i];
    for (int j = 0; j < m; j++) {
      {
        (*fi)[j][at] = max((*fi)[j][at], (*fi)[at][j] + 1);
        ans = max(ans, (*fi)[j][at]);
      }
    }
  }
  cout << ans;
  fprintf(stderr, "\n\n%.15lf\n\n",
          (double)(clock() - TIME_START) / CLOCKS_PER_SEC);
  return 0;
}
