#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "; ";
  err(++it, args...);
}
const int NMAX = 4e3 + 5;
int n, b[NMAX], a[NMAX];
vector<int> pos[NMAX];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  int x = 1;
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    if (mp.find(b[i]) == mp.end()) {
      mp[b[i]] = x;
      a[i] = x;
      x++;
    } else {
      a[i] = mp[b[i]];
    }
  }
  x--;
  for (int i = 1; i <= n; i++) {
    pos[a[i]].push_back(i);
  }
  int res = 0;
  for (int i = 1; i <= x; i++) {
    res = max(res, (int)pos[i].size());
  }
  for (int i = 1; i <= x; i++) {
    for (int j = 1; j <= x; j++) {
      if (i != j) {
        int p1 = 0, p2 = 0, cnt = 0, lst = 0;
        int take = 1;
        while (p1 < pos[i].size() || p2 < pos[j].size()) {
          if (take == 1) {
            if (p1 >= pos[i].size()) break;
            if (pos[i][p1] > lst) {
              lst = pos[i][p1];
              cnt++;
              p1++;
              take = 2;
            } else {
              p1++;
            }
          } else if (take == 2) {
            if (p2 >= pos[j].size()) break;
            if (pos[j][p2] > lst) {
              lst = pos[j][p2];
              cnt++;
              p2++;
              take = 1;
            } else {
              p2++;
            }
          }
        }
        res = max(res, cnt);
      }
    }
  }
  cout << res;
}
