#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long b[n], ans = 2;
  for (long long i = 0; i < n; i++) cin >> b[i];
  vector<long long> a;
  map<long long, long long> xx;
  a.push_back(b[0]);
  xx[b[0]]++;
  for (long long i = 1; i < n; i++) {
    xx[b[i]]++;
    if (b[i] != b[i - 1]) a.push_back(b[i]);
  }
  for (auto it : xx) ans = max(ans, it.second);
  map<long long, vector<long long> > m;
  for (long long i = 0; i < a.size(); i++) m[a[i]].push_back(i);
  for (auto it : m) {
    map<long long, long long> m1;
    vector<long long> v = it.second;
    for (long long i = 0; i < v.size() - 1; i++) {
      map<long long, long long> h;
      for (long long j = v[i] + 1; j < v[i + 1]; j++) {
        if (h[a[j]] == 0) {
          h[a[j]] = 1;
          m1[a[j]] += 2;
        }
      }
    }
    for (auto itr : m1) {
      vector<long long> tmp = m[itr.first];
      if (tmp[tmp.size() - 1] > v[v.size() - 1]) itr.second++;
      ans = max(ans, itr.second + 1);
    }
  }
  if (n == 1) ans = 1;
  cout << ans << endl;
  return 0;
}
