#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e15;
const long long int mod = 1000000007;
int frec[9][1010];
int a[1010];
int memo[1010][260];
int n, tam;
int f(int ind, int mask) {
  if (mask == ((1 << 8) - 1)) return 0;
  if (ind == n) return -1e9;
  if (memo[ind][mask] != -1) return memo[ind][mask];
  int ans = -1e9;
  for (int i = 0; i < 8; i++) {
    if (!(mask & (1 << i))) {
      int resta = 0;
      if (ind > 0) resta = frec[i + 1][ind - 1];
      int pos =
          lower_bound(frec[i + 1] + ind, frec[i + 1] + n, tam - 1 + resta) -
          frec[i + 1];
      if (pos == n) return -1e9;
      ans = max(ans, f(pos + 1, mask | (1 << i)) + tam - 1);
      pos = lower_bound(frec[i + 1] + ind, frec[i + 1] + n, tam + resta) -
            frec[i + 1];
      if (pos != n) ans = max(ans, f(pos + 1, mask | (1 << i)) + tam);
    }
  }
  return memo[ind][mask] = ans;
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 1; i <= 8; i++) {
    for (int j = 0; j < n; j++) {
      frec[i][j] = (a[j] == i);
      if (j > 0) frec[i][j] += frec[i][j - 1];
    }
  }
  int ans = 0;
  for (int i = 1; i <= 8; i++) {
    if (frec[i][n - 1] != 0) ans++;
  }
  for (int i = 2; i <= ceil((double)n / 8); i++) {
    tam = i;
    memset(memo, -1, sizeof memo);
    int aux = f(0, 0);
    ans = max(ans, aux);
  }
  cout << ans << "\n";
  return 0;
}
