#include <bits/stdc++.h>
using namespace std;
const int NN = 1010;
int a[NN], p[9], cnt[NN][9], nxt[NN][NN][9];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    for (int j = 1; j <= 8; j++) {
      cnt[i][j] = cnt[i - 1][j];
    }
    cnt[i][a[i]]++;
  }
  memset(nxt, 0x3f, sizeof nxt);
  for (int i = 0; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      for (int k = 1; k <= 8; k++) {
        nxt[i][cnt[j][k] - cnt[i][k]][k] =
            min(nxt[i][cnt[j][k] - cnt[i][k]][k], j);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= 8; i++) p[i] = i;
  do {
    int st = -1, ed = n / 8 + 2;
    while (st + 1 < ed) {
      int md = (st + ed) / 2;
      int mx = 0;
      for (int i = 0; i < 256; i++) {
        int pos = 0, k = 0;
        while (k < 8) {
          pos = nxt[pos][md + !!(i & (1 << (k)))][p[k + 1]];
          if (pos > n) break;
          k++;
        }
        if (k == 8) mx = max(mx, 8 * md + __builtin_popcount(i));
      }
      ans = max(ans, mx);
      if (mx)
        st = md;
      else
        ed = md;
      int f = 0;
      for (int i = 1; i <= 8; i++)
        if (p[i] != 9 - i) f = 1;
      if (f) continue;
    }
  } while (next_permutation(p + 1, p + 9));
  cout << ans << endl;
  return 0;
}
