#include <bits/stdc++.h>
using namespace std;
template <class _Tp>
void ckmax(_Tp &a, _Tp b) {
  if (a < b) a = b;
}
template <class _Tp>
void ckmin(_Tp &a, _Tp b) {
  if (a > b) a = b;
}
template <class _Tp>
_Tp gcd(_Tp a, _Tp b) {
  return (b == 0) ? (a) : (gcd(b, a % b));
}
int read() {
  char ch = getchar();
  bool f = 1;
  int x = 0;
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') f = 0, ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch & 15);
    ch = getchar();
  }
  return f ? x : -x;
}
const int inf = 0x3f3f3f3f;
const long long Inf = 1000000000000000000ll;
int n, cnt[9], pos[9][1050], f[1050][1 << 8], pre[9], ans, a[1050];
bool check(int mid) {
  memset(f, -inf, sizeof(f));
  memset(pre, 0, sizeof(pre));
  f[1][0] = 0;
  bool flag = false;
  for (int i = 1; i <= n; ++i) {
    for (int S = 0; S < (1 << 8); ++S) {
      if (f[i][S] == -inf) continue;
      for (int j = 1; j <= 8; ++j) {
        if (S & (1 << j - 1)) continue;
        if (pre[j] + mid > cnt[j]) continue;
        int nowpos = pos[j][pre[j] + mid];
        ckmax(f[nowpos][S | (1 << j - 1)], f[i][S]);
        if (pre[j] + mid + 1 > cnt[j]) continue;
        nowpos = pos[j][pre[j] + mid + 1];
        ckmax(f[nowpos][S | (1 << j - 1)], f[i][S] + 1);
      }
    }
    pre[a[i]]++;
  }
  for (int i = 1; i <= n + 1; ++i) {
    if (f[i][(1 << 8) - 1] >= 0) {
      flag = 1;
      ckmax(ans,
            f[i][(1 << 8) - 1] * (mid + 1) + (8 - f[i][(1 << 8) - 1]) * mid);
    }
  }
  return flag;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
  }
  for (int i = 1; i <= n; ++i) {
    pos[a[i]][++cnt[a[i]]] = i;
  }
  for (int i = 1; i <= 8; ++i)
    if (cnt[i]) ++ans;
  int l = 1, r = n / 8, mid;
  while (l <= r) {
    mid = l + r >> 1;
    if (check(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
  return 0;
}
