#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 10;
const int mod = (int)7e8 + 132213132;
const int N = (int)1e6 + 10;
const long long LLINF = (long long)1e18 + 10;
const long double pi = acos(-1.0);
const long double eps = (long double)1e-10;
template <typename T1, typename T2>
bool umin(T1 &x, const T2 &y) {
  if (x > y) return x = y, true;
  return false;
}
template <typename T1, typename T2>
bool umax(T1 &x, const T2 &y) {
  if (x < y) return x = y, true;
  return false;
}
template <typename T>
T getint() {
  char c = getchar();
  T p = 1, x = 0;
  while (c == ' ' || c == '\n') c = getchar();
  if (c == '-') p = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * p;
}
int n;
vector<int> v[N];
int dp[1010][260];
int a[N], id[N];
int solve(int x) {
  for (int i = 0; i <= n; ++i) {
    for (int j = 1; j < 256; ++j) {
      dp[i][j] = -INF;
      if (x == 0) dp[i][j] = 0;
    }
    dp[i][0] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    for (int msk = 1; msk < 256; ++msk) {
      dp[i][msk] = dp[i - 1][msk];
      int old_msk = msk ^ (1 << (a[i] - 1));
      if (msk < old_msk) continue;
      if (id[i] + 1 >= x) {
        int num = (x == 0 ? i : v[a[i]][id[i] - x + 1] - 1);
        umax(dp[i][msk], dp[num][old_msk] + x);
      }
      if (id[i] >= x) {
        int num = v[a[i]][id[i] - x] - 1;
        umax(dp[i][msk], dp[num][old_msk] + x + 1);
      }
    }
  }
  return dp[n][255];
}
int main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    id[i] = v[a[i]].size();
    v[a[i]].push_back(i);
  }
  int l = 0, r = n / 8 + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    int res = solve(mid);
    if (res > 0)
      l = mid;
    else
      r = mid;
  }
  cout << solve(l) << '\n';
  return 0;
}
