#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
int i, j, n, ans;
int a[N], f[N][300], d[20], sum[N][10];
void init() {
  d[1] = 1;
  for (i = 2; i <= 9; i++) d[i] = d[i - 1] * 2;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= 8; j++) sum[i][j] = sum[i - 1][j];
    sum[i][a[i]]++;
  }
}
void mark(int st, int k, int len, int v, int s) {
  for (i = st; i <= n; i++) {
    int t = sum[i][k] - sum[st][k];
    if (t > len) return;
    if (t == len)
      if (f[i][s] == -1 || f[i][s] < v) f[i][s] = v;
  }
}
int cal(int len) {
  int ans = -1;
  memset(f, 255, sizeof(f));
  f[0][0] = 0;
  int i, j, k;
  for (i = 0; i <= n; i++)
    for (j = 0; j <= d[9] - 1; j++)
      if (f[i][j] != -1) {
        if (j == d[9] - 1 && f[i][j] > ans) ans = f[i][j];
        for (k = 1; k <= 8; k++)
          if ((d[k] & j) == 0) {
            mark(i, k, len, f[i][j] + len, d[k] + j);
            mark(i, k, len + 1, f[i][j] + len + 1, d[k] + j);
          }
      }
  return ans;
}
void work() {
  int l = 0, r = n, mid, ans = 0, t;
  while (l <= r) {
    mid = (l + r) / 2;
    t = cal(mid);
    if (t != -1)
      l = mid + 1, ans = t;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
}
int main() {
  init();
  work();
  return 0;
}
