#include <bits/stdc++.h>
using namespace std;
const int mx = 1005;
const int mask = (1 << 8);
const int card = 8;
int dh[mx][mx][card + 5], dp[mx][mask], ori[mx], t[card + 5], t2[card + 5];
int n;
int result = 0;
void read() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &ori[i]);
    ori[i]--;
  }
}
void c() {
  for (int i = 0; i < mx; i++)
    for (int j = 0; j < mask; j++) dp[i][j] = -1;
}
void fh() {
  for (int i = 0; i < mx; i++)
    for (int j = 0; j < mx; j++)
      for (int k = 0; k < card; k++) dh[i][j][k] = -1;
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < card; k++) {
      int curr = 0;
      dh[i][0][k] = i;
      for (int j = i; j < n; j++) {
        if (ori[j] == k) dh[i][++curr][k] = j;
      }
    }
  }
}
int filldp(int len) {
  c();
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < mask; j++) {
      if (!j || dp[i][j] != -1) {
        for (int k = 0; k < card; k++) {
          if (!(j & (1 << k))) {
            int w1 = dh[i][len][k];
            int w2 = dh[i][len + 1][k];
            if (w1 != -1)
              dp[w1][j | (1 << k)] = max(dp[w1][j | (1 << k)], dp[i][j] + len);
            if (w2 != -1)
              dp[w2][j | (1 << k)] =
                  max(dp[w2][j | (1 << k)], dp[i][j] + len + 1);
          }
        }
      }
    }
  }
  int r = 0;
  for (int i = 0; i < n; i++) r = max(r, dp[i][mask - 1]);
  result = max(r, result);
  return r;
}
int main() {
  read();
  fh();
  int p = 0;
  int k = n;
  while (p < k) {
    int m = (p + k) / 2;
    int w = filldp(m);
    if (!w)
      k = m;
    else
      p = m;
    if (p + 1 == k) break;
  }
  filldp(p);
  filldp(k);
  printf("%d\n", result);
}
