#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T BMOD(T p, T e, T m) {
  T ret = 1;
  while (e) {
    if (e & 1) ret = (ret * p) % m;
    p = (p * p) % m;
    e >>= 1;
  }
  return (T)ret;
}
template <typename T>
inline T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return SQR(POW(B, P / 2));
}
template <typename T>
inline T ModInv(T b, T m) {
  return BMOD(b, m - 2, m);
}
template <typename T>
inline T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T>
inline T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int fr[9], n, arr[10000], pre[1005][10], dp[1002][(1 << 8) + 2];
int nxt(int pos, int sz, int val) {
  int lo = pos, hi = n, mid, ans = -1;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    if (pre[mid][val] - pre[pos - 1][val] >= sz) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  return ans;
}
int go(int pos, int mask, int sz) {
  if (mask == (1 << 8) - 1) return 0;
  if (pos > n) return -100000;
  if (dp[pos][mask] != -1) return dp[pos][mask];
  int ret = -100000;
  for (int i = 0; i < 8; i++) {
    if ((mask & (1 << i))) continue;
    int id = nxt(pos, sz, i);
    if (id == -1) {
      if (sz > 1) {
        id = nxt(pos, sz - 1, i);
        if (id != -1) ret = max(ret, sz - 1 + go(id + 1, mask | (1 << i), sz));
      } else
        ret = max(ret, go(pos, mask | (1 << i), sz));
    } else {
      ret = max(ret, sz + go(id + 1, mask | (1 << i), sz));
      if (sz > 1) {
        id = nxt(pos, sz - 1, i);
        if (id != -1) ret = max(ret, sz - 1 + go(id + 1, mask | (1 << i), sz));
      } else
        ret = max(ret, go(pos, mask | (1 << i), sz));
    }
  }
  return dp[pos][mask] = ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    arr[i]--;
    for (int j = 0; j < 8; j++) pre[i][j] = pre[i - 1][j];
    pre[i][arr[i]] += 1;
  }
  int res = 0;
  for (int i = 1; i <= (1000 / 8); i++) {
    memset(dp, -1, sizeof dp);
    res = max(res, go(1, 0, i));
  }
  printf("%d", res), puts("");
  return 0;
}
