#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int inf = 1000000009;
int rd() {
  char c = getchar();
  int t = 0, f = 1;
  while (!isdigit(c)) f = (c == '-') ? -1 : 1, c = getchar();
  while (isdigit(c)) t = t * 10 + c - 48, c = getchar();
  return t * f;
}
void wt(int x) {
  if (x < 0) {
    putchar('-');
    wt(-x);
    return;
  }
  if (x > 9) wt(x / 10);
  putchar(x % 10 + 48);
  return;
}
int num[9], A[N], flag, G[9], pos[9][N], F[N][1 << 8], cnt[9], n, Ans, Res, sum;
bool cheat() {
  memset(num, 0, sizeof num);
  for (int i = ((int)1); i <= ((int)n); i++) num[A[i]]++;
  for (int i = ((int)1); i <= ((int)8); i++)
    if (num[i] < G[i]) return 0;
  return 1;
}
bool check() {
  int ps;
  for (int i = ((int)1); i <= ((int)8); i++)
    if (!G[i]) return cheat();
  memset(F, 0, sizeof F);
  memset(cnt, 0, sizeof cnt);
  memset(num, 0, sizeof num);
  for (int i = ((int)1); i <= ((int)n); i++) pos[A[i]][++num[A[i]]] = i;
  F[0][0] = 1;
  for (int i = ((int)1); i <= ((int)n); i++) {
    cnt[A[i]]++;
    memcpy(F[i], F[i - 1], sizeof F[i]);
    if (cnt[A[i]] < G[A[i]]) continue;
    for (int j = ((int)0); j <= ((int)(1 << 8) - 1); j++)
      if (~j & (1 << A[i] - 1)) {
        ps = pos[A[i]][cnt[A[i]] - G[A[i]] + 1] - 1;
        F[i][j | (1 << A[i] - 1)] |= F[ps][j];
      }
  }
  return F[n][(1 << 8) - 1];
}
int main() {
  n = rd();
  for (int i = ((int)1); i <= ((int)n); i++) A[i] = rd();
  for (int i = ((int)1); i <= ((int)8); i++) G[i] = 2;
  check();
  for (int l = 1, r = n / 8; l <= r;) {
    int mid = l + r >> 1;
    for (int i = ((int)1); i <= ((int)8); i++) G[i] = mid;
    if (check())
      Ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  Res = 8 * Ans;
  for (int i = ((int)0); i <= ((int)(1 << 8) - 1); i++) {
    sum = 0;
    for (int j = ((int)0); j <= ((int)7); j++)
      if (i >> j & 1)
        G[j + 1] = Ans + 1;
      else
        G[j + 1] = Ans;
    for (int j = ((int)0); j <= ((int)7); j++) sum += G[j + 1];
    if (sum <= Res) continue;
    if (check()) Res = sum;
  }
  printf("%d\n", Res);
  return 0;
}
