#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const double eps = 1e-7;
const double PI = acos(-1.);
const double E = 2.71828182845904523536;
const int MOD = (int)1e9;
const int INF = 0x7f7f7f7f;
const int N = 2e5 + 25;
const int M = 1200000 + 5;
inline int clz(int x) { return __builtin_clz(x); }
inline int clz(long long x) { return __builtin_clzll(x); }
inline int lg2(int x) { return !x ? -1 : 31 - clz(x); }
inline int lg2(long long x) { return !x ? -1 : 63 - clz(x); }
int n, a[1000 + 7], g[1000 + 7][1000 + 7][8], dp[1 << 8][10], cnt[10], ans;
void umin(int& a, int b) {
  if (a > b) a = b;
}
int main() {
  ans = -1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i]--;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 0; k < 8; k++) g[i][j][k] = n + 10;
    }
    for (int j = 0; j <= 8; j++) cnt[j] = 0;
    for (int j = i; j <= n; j++) g[i][++cnt[a[j]]][a[j]] = j;
  }
  for (int x = 0; x * 8 <= n; x++) {
    for (int mask = 0; mask < 1 << 8; mask++)
      for (int i = 0; i < 9; i++) dp[mask][i] = n + 10;
    dp[0][0] = 1;
    for (int mask = 0; mask < 1 << 8; mask++) {
      for (int o = 0; o <= 8; o++) {
        if (dp[mask][o] >= n + 1) continue;
        for (int k = 0; k < 8; k++) {
          if (mask & (1 << k)) continue;
          umin(dp[mask | (1 << k)][o], g[dp[mask][o]][x][k] + 1);
          umin(dp[mask | (1 << k)][o + 1], g[dp[mask][o]][x + 1][k] + 1);
        }
      }
    }
    for (int i = 0; i <= 8; i++) {
      if (dp[255][i] <= n + 1) {
        ans = max(ans, x * (8 - i) + i * (x + 1));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
