#include <bits/stdc++.h>
using namespace std;
int a[1010];
int to[1010][9][1010];
int seq[50010][8];
int tot, n;
bool check(int x) {
  for (int i = 1; i <= tot; i++) {
    int loc = 1;
    for (int j = 0; j < 8; j++) {
      int y = seq[i][j];
      loc = to[loc][y][x];
      if (loc == -1) break;
    }
    if (loc != -1) return true;
  }
  return false;
}
int dp[1010][256];
int cal(int x) {
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= 255; j++)
      if (dp[i][j] >= 0)
        for (int k = 1; k <= 8; k++) {
          int state = j & (1 << (k - 1)), nstate = j | (1 << (k - 1));
          if (state == 0) {
            if (to[i][k][x] != -1)
              dp[to[i][k][x]][nstate] =
                  max(dp[to[i][k][x]][nstate], dp[i][j] + x);
            if (to[i][k][x + 1] != -1)
              dp[to[i][k][x + 1]][nstate] =
                  max(dp[to[i][k][x + 1]][nstate], dp[i][j] + x + 1);
          }
        }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (x == 0)
      for (int j = 0; j <= 255; j++) ans = max(ans, dp[i][j]);
    else
      ans = max(ans, dp[i][255]);
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  tot = 1;
  for (int i = 1; i <= 8; i++) tot = tot * i, seq[1][i - 1] = i;
  for (int i = 2; i <= tot; i++) {
    for (int j = 0; j < 8; j++) seq[i][j] = seq[i - 1][j];
    next_permutation(seq[i], seq[i] + 8);
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  memset(to, -1, sizeof(to));
  for (int i = 0; i <= n; i++) {
    for (int k = 1, tmp = 0; k <= 8; k++) {
      tmp = 0;
      for (int j = i; j <= n; j++)
        if (k == a[j]) {
          tmp++;
          to[i][k][tmp] = j;
        }
    }
  }
  int l = 0, r = n / 8 + 8, mid, ans = 0;
  while (l < r) {
    mid = l + r >> 1;
    if (check(mid)) {
      ans = mid;
      l = mid + 1;
    } else
      r = mid;
  }
  l = ans;
  ans = 1;
  for (int i = l - 2; i <= l + 2; i++)
    if (i >= 0 && i <= n) ans = max(ans, cal(i));
  printf("%d\n", ans);
  return 0;
}
