#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline long long read() {
  long long f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + (s - '0');
    s = getchar();
  }
  return x * f;
}
const int jzm = 1000000007;
int n, m, i, j, s, o, k;
int a[1005], Sm;
int nm[1005][1005][10];
int pre[1005][10][2];
bool f[1005][1 << 8 | 5];
int dp[1005][1 << 8 | 5];
bool check(int m) {
  memset(f, 0, sizeof(f));
  memset(dp, 0, sizeof(dp));
  memset(pre, 0, sizeof(pre));
  int tp = (1 << 8) - 1;
  if (m == 0) {
    dp[n][tp] = Sm;
    return 1;
  }
  f[0][0] = 1;
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    f[i][0] = 1;
    dp[i][0] = 0;
    for (int j = 1; j <= 8; j++) {
      bool ff = 1;
      pre[i][j][0] = i + 1;
      for (int k = i; k >= 1; k--) {
        if (nm[k][i][j] > m) ff = 0;
        if (ff) pre[i][j][0] = k;
        pre[i][j][1] = k;
        if (nm[k][i][j] >= m) ff = 0;
        if (nm[k][i][j] > m) break;
      }
      if (nm[1][i][j] <= m) pre[i][j][1] = 0;
      if (nm[1][i][j] < m) pre[i][j][0] = 0;
    }
    for (int j = 1; j <= tp; j++) {
      for (int k = 1; k <= 8; k++) {
        if (j & (1 << (k - 1))) {
          int k2 = j ^ (1 << (k - 1));
          if (pre[i][k][0] && f[pre[i][k][0] - 1][k2]) {
            f[i][j] = 1;
            dp[i][j] = max(dp[pre[i][k][0] - 1][k2] + m, dp[i][j]);
          }
          if (pre[i][k][1] && f[pre[i][k][1] - 1][k2]) {
            f[i][j] = 1;
            dp[i][j] = max(dp[pre[i][k][1] - 1][k2] + m + 1, dp[i][j]);
          }
        }
      }
    }
  }
  return f[n][tp];
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    for (int j = i; j > 0; j--) {
      for (int k = 1; k <= 8; k++) {
        nm[j][i][k] = nm[j + 1][i][k];
      }
      nm[j][i][a[j]]++;
    }
  }
  for (int i = 1; i <= 8; i++) {
    if (nm[1][n][i]) Sm++;
  }
  int as = 0;
  for (int i = (1 << 7); i > 0; i >>= 1) {
    if (check(as + i)) as += i;
  }
  check(as);
  int tp = (1 << 8) - 1;
  printf("%d\n", dp[n][tp]);
  return 0;
}
