#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
bool compare(T x, U y) {
  return (abs(x - y) <= 1e-9);
}
const int MOD = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  int a[n];
  for (auto &x : a) cin >> x;
  if (n == 1) {
    cout << 1;
    return;
  }
  vector<vector<int> > dp(n, vector<int>(n));
  int ans = -1;
  map<int, vector<int> > mp;
  for (int i = 0; i < n; i++) {
    mp[a[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    dp[i][i] = 1;
    for (int j = 0; j < i; j++) {
      int a1 = a[i];
      int a2 = a[j];
      if (a1 == a2) {
        auto it = lower_bound(mp[a1].begin(), mp[a1].end(), j);
        if (it == mp[a1].begin()) {
          dp[i][j] = 2;
        } else {
          it--;
          int id_a1 = *it;
          auto it1 = lower_bound(mp[a1].begin(), mp[a1].end(), id_a1);
          if (it1 == mp[a1].begin())
            dp[i][j] = 3;
          else {
            it1--;
            int id_a2 = *it1;
            dp[i][j] = dp[id_a1][id_a2] + 2;
          }
        }
        ans = max(ans, dp[i][j]);
        continue;
      }
      auto it = lower_bound(mp[a1].begin(), mp[a1].end(), j);
      if (it == mp[a1].begin()) {
        dp[i][j] = 2;
        ans = max(ans, dp[i][j]);
        continue;
      } else if (it != mp[a1].end())
        it--;
      else {
        dp[i][j] = 2;
        ans = max(ans, dp[i][j]);
        continue;
      }
      int id_a1 = *it;
      auto it1 = lower_bound(mp[a2].begin(), mp[a2].end(), id_a1);
      if (it1 == mp[a2].begin()) {
        dp[i][j] = 3;
        ans = max(ans, dp[i][j]);
        continue;
      } else if (it1 != mp[a2].end()) {
        it1--;
        int id_a2 = *it1;
        dp[i][j] = dp[id_a1][id_a2] + 2;
      } else
        dp[i][j] = 2;
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
    cout << "\n";
  }
}
