#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, a[1005], c[9][1005] = {0};
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) c[a[i]][i]++;
  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= n; j++) c[i][j] += c[i][j - 1];
  }
  int lo = 1, hi = n, mid, ans;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    int b[8];
    for (int i = 0; i <= 7; i++) b[i] = i + 1;
    int f = 0;
    do {
      int x = mid / 8, y = mid - 8 * x;
      for (int msk = 0; msk <= (1 << 8) - 1; msk++) {
        if (__builtin_popcount(msk) == y) {
          int lst = 0, f1 = 1;
          for (int i = 0; i <= 7; i++) {
            int cnt;
            if (msk & (1 << i))
              cnt = x + 1;
            else
              cnt = x;
            if (c[b[i]][n] - c[b[i]][lst] < cnt) {
              f1 = 0;
              break;
            }
            int l1 = lst + 1, h1 = n, m1, kk;
            while (l1 <= h1) {
              m1 = (l1 + h1) / 2;
              if (c[b[i]][m1] - c[b[i]][lst] >= cnt)
                h1 = m1 - 1, kk = m1;
              else
                l1 = m1 + 1;
            }
            lst = kk;
          }
          f |= f1;
          if (f) break;
        }
      }
      if (f) break;
    } while (next_permutation(b, b + 8));
    if (f) {
      ans = mid, lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  cout << ans;
  return 0;
}
