#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
int last[maxn + 50][maxn + 50], f[maxn + 50][256][8];
int a[maxn + 50];
int n, ans = 0;
bool check(int len) {
  if (len == 0) {
    int g[9];
    memset(g, 0, sizeof(g));
    for (int i = 1; i <= n; ++i) g[a[i]] = 1;
    int s = 0;
    for (int i = 1; i <= 8; ++i)
      if (g[i]) ++s;
    ans = max(ans, s);
    return 1;
  }
  memset(f, 0, sizeof(f));
  f[0][0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < 256; ++j)
      for (int k = 0; k < 8; ++k) {
        f[i][j][k] = f[i - 1][j][k];
        if (((1 << (a[i] - 1)) & j) == 0) continue;
        if (last[i][len])
          f[i][j][k] |= f[last[i][len] - 1][j - (1 << (a[i] - 1)) & j][k];
        if (k > 0 && last[i][len + 1])
          f[i][j][k] |=
              f[last[i][len + 1] - 1][j - (1 << (a[i] - 1)) & j][k - 1];
      }
  bool flag = 0;
  for (int j = 0; j < 8; ++j)
    if (f[n][255][j]) {
      flag = 1;
      ans = max(ans, len * 8 + j);
    }
  return flag;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) last[i][1] = i;
  for (int k = 1; k <= 8; ++k) {
    int fa = 0;
    for (int i = 1; i <= n; ++i)
      if (a[i] == k) {
        last[i][2] = fa;
        fa = i;
      }
  }
  for (int k = 3; k <= n; ++k)
    for (int i = 1; i <= n; ++i) last[i][k] = last[last[i][k - 1]][2];
  int l = 0, r = n - 1, mid;
  while (l < r) {
    mid = (l + r + 1) >> 1;
    if (check(mid))
      l = mid;
    else
      r = mid - 1;
  }
  check(l);
  printf("%d", ans);
  return 0;
}
