#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T sqr(T a) {
  return a * a;
};
const double pi = acos(-1.0);
const int INF = INT_MAX;
const int MAX_N = 1000 + 10;
int N, A[MAX_N], cur[10], dp[MAX_N][1 << 8];
vector<int> in[MAX_N];
int can(int len) {
  if (!len) {
    int res = 0;
    for (int i = 0; i < 8; ++i)
      if (in[i].size()) ++res;
    return res;
  }
  for (int i = 0; i < 8; ++i) cur[i] = 0;
  for (int i = 0; i < N + 1; ++i)
    for (int S = 0; S < 1 << 8; ++S) dp[i][S] = -INF;
  dp[0][0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int S = 0; S < 1 << 8; ++S)
      if (dp[i][S] != -INF)
        for (int k = 0; k < 8; ++k)
          if (!(S >> k & 1)) {
            int it = cur[k] + len - 1;
            if (it >= in[k].size()) continue;
            dp[in[k][it] + 1][S | (1 << k)] =
                max(dp[in[k][it] + 1][S | (1 << k)], dp[i][S]);
            it++;
            if (it >= in[k].size()) continue;
            dp[in[k][it] + 1][S | (1 << k)] =
                max(dp[in[k][it] + 1][S | (1 << k)], dp[i][S] + 1);
          }
    ++cur[A[i]];
  }
  int ans = -INF;
  for (int i = 0; i < N + 1; ++i) ans = max(ans, dp[i][(1 << 8) - 1]);
  if (ans == -INF) return -1;
  return ans * (len + 1) + (8 - ans) * len;
}
int main(int argc, char const *argv[]) {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) scanf("%d", A + i), --A[i], in[A[i]].push_back(i);
  int lb = 0, ub = N / 8 + 1;
  while (lb + 1 < ub) {
    int mid = (lb + ub) >> 1;
    if (~can(mid))
      lb = mid;
    else
      ub = mid;
  }
  printf("%d\n", can(lb));
  return 0;
}
