#include <bits/stdc++.h>
using namespace std;
int bit[12], f[1005][1005], n, a[1005], s[1005][10], lst[1005][1005],
    dst[1005][1005], ans;
int print(int x) {
  int tot = 0, p[12];
  while (x) {
    p[++tot] = x % 2;
    x /= 2;
  }
  for (int i = tot; i > 0; i--) printf("%d", p[i]);
}
int getnum(int x) {
  int ans = 0, p;
  while (x) {
    p = x % 2;
    x /= 2;
    if (p == 1) ans++;
  }
  return ans;
}
int main() {
  int i, j, k, x, maxn, minn, summ;
  memset(lst, -1, sizeof lst);
  memset(dst, 127, sizeof dst);
  bit[0] = 1;
  for (i = 1; i <= 10; i++) bit[i] = bit[i - 1] << 1;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    for (j = 1; j <= 8; j++) s[i][j] = s[i - 1][j];
    s[i][a[i]]++;
  }
  for (i = 1; i <= 8; i++)
    if (s[n][i]) ans++;
  for (i = 1; i <= n; i++) {
    f[i][bit[a[i] - 1]] = s[i][a[i]];
    lst[i][bit[a[i] - 1]] = s[i][a[i]];
    dst[i][bit[a[i] - 1]] = s[i][a[i]];
  }
  for (i = 2; i <= n; i++) {
    x = a[i];
    for (j = 1; j < i; j++) {
      if (a[j] == x) continue;
      for (k = 1; k < bit[8]; k++) {
        if ((k & bit[x - 1]) || (!f[j][k])) continue;
        maxn = lst[j][k];
        minn = dst[j][k];
        summ = s[i][x] - s[j][x];
        if (summ < maxn - 1) {
          if ((summ + 1) * getnum(k) + summ > f[i][k | bit[x - 1]]) {
            f[i][k | bit[x - 1]] = (summ + 1) * getnum(k) + summ;
            lst[i][k | bit[x - 1]] = summ + 1;
            dst[i][k | bit[x - 1]] = summ;
          }
          continue;
        }
        if (summ > minn + 1) {
          if (f[j][k] + minn + 1 > f[i][k | bit[x - 1]]) {
            f[i][k | bit[x - 1]] = f[j][k] + minn + 1;
            lst[i][k | bit[x - 1]] = minn + 1;
            dst[i][k | bit[x - 1]] = minn;
          }
          continue;
        } else if (f[j][k] + summ > f[i][k | bit[x - 1]]) {
          f[i][k | bit[x - 1]] = f[j][k] + summ;
          lst[i][k | bit[x - 1]] = max(maxn, summ);
          dst[i][k | bit[x - 1]] = min(minn, summ);
        }
      }
    }
  }
  for (i = 1; i <= n; i++) {
    ans = max(ans, f[i][bit[8] - 1]);
  }
  printf("%d\n", ans);
}
