#include <bits/stdc++.h>
using namespace std;
int n;
int dp[10][310];
int nex[1010][10][1010];
int ai[1010];
int up;
void init(void) {
  memset(dp, -1, sizeof(dp));
  int i, j;
  int cnt[10];
  for (i = 1; i <= n; i++) {
    memset(cnt, 0, sizeof(cnt));
    for (j = 1; j <= 8; j++)
      for (int k = 1; k <= n; k++) nex[i][j][k] = 1e9;
    for (j = i; j <= n; j++) {
      cnt[ai[j]]++;
      nex[i][ai[j]][cnt[ai[j]]] = j;
    }
  }
}
int main() {
  int i, j, k;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ai[i];
  }
  init();
  up = (1 << 8) - 1;
  int ans = 0;
  for (int num = 0; num * 8 <= n; num++) {
    for (int i = 0; i < 256; i++)
      for (int k = 0; k <= 8; k++) dp[k][i] = 1e9;
    dp[0][0] = 1;
    for (i = 0; i <= up; i++) {
      for (j = 0; j <= 8; j++) {
        for (k = 1; k <= 8; k++) {
          if (i & (1 << (k - 1))) continue;
          if (dp[j][i] > n) continue;
          dp[j][i ^ (1 << (k - 1))] =
              min(dp[j][i ^ (1 << (k - 1))], nex[dp[j][i]][k][num] + 1);
          dp[j + 1][i ^ (1 << (k - 1))] =
              min(dp[j + 1][i ^ (1 << (k - 1))], nex[dp[j][i]][k][num + 1] + 1);
        }
      }
    }
    for (i = 0; i <= 8; i++)
      if (dp[i][up] <= n + 1) {
        ans = max(ans, num * 8 + i);
      }
  }
  cout << ans;
  return 0;
}
