#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> ma[10];
int a[1010], p[10], bit[10], f[1010][1 << 8];
inline int check(int s) {
  memset(bit, 0, sizeof(bit));
  memset(f, -1, sizeof(f));
  f[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << 8); j++) {
      if (f[i][j] == -1) continue;
      for (int k = 1; k <= 8; k++) {
        if (j & (1 << (k - 1))) continue;
        int x = bit[k] + s - 1;
        if (x >= ma[k].size()) continue;
        f[ma[k][x]][j | (1 << (k - 1))] =
            max(f[ma[k][x]][j | (1 << (k - 1))], f[i][j]);
        if ((++x) >= ma[k].size()) continue;
        f[ma[k][x]][j | (1 << (k - 1))] =
            max(f[ma[k][x]][j | (1 << (k - 1))], f[i][j] + 1);
      }
    }
    ++bit[a[i]];
  }
  int now = -1;
  for (int i = 1; i <= n; i++) now = max(now, f[i][(1 << 8) - 1]);
  if (now == -1) return -1;
  return now * (s + 1) + (8 - now) * s;
}
void Solve() {
  int l = 1, r = n / 8, mid;
  while (l + 1 < r) {
    mid = (l + r) >> 1;
    if (check(mid) != -1)
      l = mid;
    else
      r = mid - 1;
  }
  int now = max(check(l), check(r));
  if (now == -1) {
    now = 0;
    for (int i = 1; i <= 8; i++)
      if (ma[i].size()) now++;
  }
  printf("%d\n", now);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ma[a[i]].push_back(i);
  }
  Solve();
  return 0;
}
