#include <bits/stdc++.h>
using namespace std;
int f[1 << 8 | 1][9], n;
int N[1001][9][1001];
int x[1001], cnt[9];
int bin[10];
int ans;
int main() {
  scanf("%d", &n);
  bin[0] = 1;
  for (int i = 1; i <= 8; i++) bin[i] = bin[i - 1] << 1;
  for (int i = 1; i <= n; i++) scanf("%d", &x[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 8; j++) {
      cnt[j] = 0;
      for (int k = 1; k <= n; k++) N[i][j][k] = 1e9;
    }
    for (int j = i; j <= n; j++) {
      ++cnt[x[j]];
      N[i][x[j]][cnt[x[j]]] = j;
    }
  }
  for (int i = 0; i * 8 <= n; i++) {
    memset(f, 0x3f, sizeof(f));
    f[0][0] = 1;
    for (int j = 0; j < bin[8]; j++)
      for (int k = 0; k <= 8; k++)
        for (int l = 1; l <= 8; l++) {
          if ((bin[l - 1] & j) || f[j][k] > n) continue;
          f[j ^ bin[l - 1]][k] =
              min(f[j ^ bin[l - 1]][k], N[f[j][k]][l][i] + 1);
          f[j ^ bin[l - 1]][k + 1] =
              min(f[j ^ bin[l - 1]][k + 1], N[f[j][k]][l][i + 1] + 1);
        }
    for (int j = 0; j <= 8; j++)
      if (f[bin[8] - 1][j] <= n + 1) ans = max(ans, i * 8 + j);
  }
  printf("%d", ans);
}
