#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
  for (int i(0); i < N; i++) scanf("%d", A + i);
}
void pc(int *s, int xx[(1003)][8], int L) {
  for (int i(0); i < 8; i++) xx[L][i] = -1;
  for (int i(L - 1); ~i; --i) {
    for (int j(0); j < 8; j++) xx[i][j] = xx[i + 1][j];
    xx[i][s[i]] = i;
  }
}
int A[(1003)], N, X[(130)][(1003)][8], K, dp[(1003)][1 << 8];
int dyn(int u, int b) {
  if (b == (1 << 8) - 1) return 0;
  if (u >= N) return -1e9;
  int &v = dp[u][b];
  if (~v) return v;
  for (int i(0); i < 8; i++)
    if (!(b & 1 << i) && !~X[K - 1][u][i]) return -1e9;
  v = -1e9;
  for (int i(0); i < 8; i++)
    if (!(b & 1 << i)) v = max(v, K + dyn(X[K - 1][u][i] + 1, b | 1 << i));
  for (int i(0); i < 8; i++)
    if (!(b & 1 << i) && ~X[K][u][i])
      v = max(v, 1 + K + dyn(X[K][u][i] + 1, b | 1 << i));
  return v;
}
bool ok(long long t) {
  if (!t) return 0;
  (memset(dp, -1, sizeof(dp))), K = t;
  return dyn(0, 0) < 0;
}
int bs(int B = 0, int E = (1 << 29)) {
  int M;
  while (B < E)
    if (ok(M = (B + E) >> 1))
      E = M;
    else
      B = M + 1;
  return max(B - 1, 0);
}
int dw() {
  set<int> S;
  for (int i(0); i < N; i++) S.insert(A[i]);
  return S.size();
}
int main(void) {
  scanf("%d", &N), ga(N, A), (memset(X, -1, sizeof(X)));
  for (int i(0); i < N; i++) --A[i];
  pc(A, *X, N);
  for (int k(1); k < (130); k++)
    for (int i = N - 1; ~i; --i)
      for (int j(0); j < 8; j++) {
        if (~X[k - 1][i][j])
          X[k][i][j] = X[0][X[k - 1][i][j] + 1][j];
        else
          X[k][i][j] = -1;
      }
  K = bs(0, (130) - 1), (memset(dp, -1, sizeof(dp)));
  printf("%d\n", K ? dyn(0, 0) : dw());
  return 0;
}
