#include <bits/stdc++.h>
using namespace std;
int dp[1010][260];
int n, lim, L, R, mid, ans, now, tmp;
int seq[1010], ord[1010], pos[10][1010], cnt[10];
bool Judge(int k) {
  tmp = 0;
  memset(dp, 128, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= lim; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j & (1 << seq[i])) {
        now = ord[i];
        if (k && now >= k)
          dp[i][j] =
              max(dp[i][j],
                  dp[pos[seq[i]][now - k + 1] - 1][j - (1 << seq[i])] + k);
        if (now >= k + 1)
          dp[i][j] =
              max(dp[i][j],
                  dp[pos[seq[i]][now - k] - 1][j - (1 << seq[i])] + k + 1);
      }
    }
  }
  if (k)
    tmp = dp[n][lim];
  else
    for (int i = 0; i <= lim; i++) tmp = max(tmp, dp[n][i]);
  return tmp > 0;
}
int main(void) {
  scanf("%d", &n);
  lim = (1 << 8) - 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", seq + i);
    ord[i] = ++cnt[--seq[i]], pos[seq[i]][cnt[seq[i]]] = i;
  }
  L = 0, R = n / 8;
  while (L <= R) {
    mid = (L + R) >> 1;
    Judge(mid) ? L = mid + 1, ans = tmp : R = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}
