#include <bits/stdc++.h>
const short INF = 32639;
const double EPS = 1e-10;
const long long mod7 = 1e9 + 7;
const long long mod9 = 1e9 + 9;
using namespace std;
inline long long rit() {
  long long f = 0, key = 1;
  char c;
  do {
    c = getchar();
    if (c == '-') key = -1;
  } while (c < '0' || c > '9');
  do {
    f = f * 10 + c - '0';
    c = getchar();
  } while (c >= '0' && c <= '9');
  return f * key;
}
inline void fprt(double f) { printf("%.08lf", f); }
void init() {}
const int Z = 1005;
int n;
short a[Z], ans, all;
short dp[Z][300], dp2[8][Z][Z];
void do_dp2() {
  int st, ed, cnt, cnt2;
  memset((dp2), 127, sizeof(dp2));
  for (short i = 0; i < 8; i++) {
    for (short j = 0; j < n; j++) dp2[i][j][0] = j + 1;
  }
  for (short i = 0; i < 8; i++) {
    cnt = 0;
    for (short r = 0; r < n; r++) {
      if (a[r] == i) cnt++;
      cnt2 = cnt;
      for (short j = 0; j < r + 1; j++) {
        dp2[i][j][cnt2] = min(dp2[i][j][cnt2], (short)(r + 1));
        if (a[j] == i) cnt2--;
      }
    }
  }
}
inline void prt(int x) {
  for (short i = 0; i < 8; i++) cout << (x & (1 << i) ? 1 : 0);
}
inline int chk(short x) {
  int key;
  memset((dp), -1, sizeof((dp)));
  dp[0][0] = key = 0;
  for (short i = 0; i < n + 1; i++) {
    for (short j = 0; j < 256; j++) {
      if (dp[i][j] != -1) {
        for (short k = 0; k < 8; k++) {
          if (!(j & (1 << k))) {
            if (dp2[k][i][x] != INF) {
              dp[dp2[k][i][x]][j + (1 << k)] =
                  max(dp[dp2[k][i][x]][j + (1 << k)], (short)(dp[i][j] + x));
            }
            if (x > 0 && dp2[k][i][x - 1] != INF) {
              dp[dp2[k][i][x - 1]][j + (1 << k)] =
                  max(dp[dp2[k][i][x - 1]][j + (1 << k)],
                      (short)(dp[i][j] + x - 1));
            }
          }
        }
        if (j == 255) {
          ans = max(ans, dp[i][j]);
          key = 1;
        }
      }
    }
  }
  return key;
}
void read() {
  n = rit();
  ;
  all = 0;
  for (short i = 0; i < n; i++) {
    a[i] = rit() - 1;
    all |= (1 << a[i]);
  }
}
void solve() {
  short p, q;
  if (n <= 8) {
    cout << __builtin_popcount((int)all) << endl;
    return;
  }
  do_dp2();
  p = 0;
  q = n / 8 + 1;
  while (p <= q) {
    short mid = (p + q) / 2;
    if (chk(mid))
      p = mid + 1;
    else
      q = mid - 1;
  }
  cout << ans << endl;
}
int main() {
  int nn = 1;
  while (nn--) {
    init(), read(), solve();
  }
  return 0;
}
