#include <bits/stdc++.h>
using namespace std;
int n;
int a[1200];
int dp[1200][1000];
int nxt[1200][8];
int find_nx(int start, int tt, int x) {
  if (start > n) return -1;
  for (int i = 0; i < tt; ++i) {
    start = nxt[start][x];
    if (start > n) return -1;
  }
  return start;
}
int check(int len) {
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 0; i <= n; ++i)
    for (int j = 0; j < 8; ++j) {
      int stop = find_nx(i, len, j);
      if (stop == -1) continue;
      for (int k = 0; k < 1 << 8; ++k)
        if (dp[i][k] != -1 && ((1 << j) & k) == 0)
          dp[stop][(1 << j) | k] = max(dp[stop][(1 << j) | k], dp[i][k]);
      stop = nxt[stop][j];
      if (stop == -1) continue;
      for (int k = 0; k < 1 << 8; ++k)
        if (dp[i][k] != -1 && ((1 << j) & k) == 0)
          dp[stop][(1 << j) | k] = max(dp[stop][(1 << j) | k], dp[i][k] + 1);
    }
  int res = -1;
  for (int i = 1; i <= n; ++i) {
    if (dp[i][255] != -1) res = max(res, dp[i][255]);
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    a[i]--;
  }
  for (int i = 0; i < 8; ++i) nxt[n][i] = n + 1;
  for (int i = 0; i < 8; ++i)
    for (int j = n - 1; j >= 0; --j) {
      if (a[j + 1] == i)
        nxt[j][i] = j + 1;
      else
        nxt[j][i] = nxt[j + 1][i];
    }
  int L = 0, R = n;
  while (L < R) {
    int M = (L + R + 1) / 2;
    if (check(M) != -1)
      L = M;
    else
      R = M - 1;
  }
  int res = check(L);
  int ans = res * (L + 1) + (8 - res) * L;
  printf("%d\n", ans);
  return 0;
}
