#include <bits/stdc++.h>
using namespace std;
int nxt[1111][10], after[1111][1111];
int a[1111], cnt[10], r[10], vst[10];
int maxL, N;
int main() {
  int i, k, j, u;
  int mincnt = 0x3f3f3f3f;
  cin >> N;
  memset(nxt, 0x3f, sizeof(nxt));
  memset(after, 0x3f, sizeof(after));
  memset(vst, 0, sizeof(vst));
  for (i = 0; i < N; i++) {
    cin >> a[i];
    vst[a[i]] = 1;
  }
  for (i = 0; i < 8; i++) r[i] = i + 1;
  for (i = N - 1; i >= 0; i--) {
    for (k = 1; k <= 8; k++) {
      if (a[i] == k)
        nxt[i][k] = i;
      else
        nxt[i][k] = nxt[i + 1][k];
    }
  }
  for (i = N - 1; i >= 0; i--) {
    after[i][0] = i - 1;
    after[i][1] = i;
    if (nxt[i + 1][a[i]] >= N) continue;
    for (k = 2; k < N; k++) after[i][k] = after[nxt[i + 1][a[i]]][k - 1];
  }
  maxL = 0;
  for (i = 1; i <= 8; i++)
    if (vst[i]) maxL++;
  do {
    int st = 0, ed = N / 8 + 1, md, mx;
    while (st + 1 < ed) {
      md = (st + ed) >> 1;
      mx = 0;
      for (i = 0; i < 1 << 8; i++) {
        for (k = 0, u = 0; k < 8; k++) {
          if (md == 0 && (1 & (1 << k)) == 0) continue;
          u = nxt[u][r[k]];
          if (u > N) break;
          u = after[u][md + ((i & (1 << k)) > 0)];
          if (u > N) break;
          u++;
        }
        if (k == 8) mx = max(mx, __builtin_popcount(i) + md * 8);
      }
      if (mx > 0) {
        maxL = max(maxL, mx);
        st = md;
      } else
        ed = md;
    }
  } while (next_permutation(r, r + 8));
  cout << maxL << endl;
  return 0;
}
