#include <bits/stdc++.h>
using namespace std;
int n, x, sum[1010][1010], dp[1010][1 << 8];
const int m = 1 << 8;
int ans;
int find(int x, int y, int l) {
  int r = n, mid;
  if (sum[l][y] >= x) return l;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (sum[mid - 1][y] < x && sum[mid][y] >= x) return mid;
    if (x > sum[mid][y])
      l = mid + 1;
    else
      r = mid - 1;
  }
  return -1;
}
int Soviet(int len) {
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < m; j++)
      if (dp[i][j]) {
        for (int k = 0; k < 8; k++)
          if (!(j & (1 << k))) {
            int x1 = find(sum[i][k + 1] + len, k + 1, i);
            int x2 = find(sum[i][k + 1] + len + 1, k + 1, i);
            if (x1 != -1)
              dp[x1][j | (1 << k)] = max(dp[x1][j | (1 << k)], dp[i][j] + len);
            if (x2 != -1)
              dp[x2][j | (1 << k)] =
                  max(dp[x2][j | (1 << k)], dp[i][j] + len + 1);
          }
      }
  }
  int ret = 0;
  for (int i = 1; i <= n; i++) ret = max(ret, dp[i][m - 1]);
  return ret - 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    for (int j = 1; j <= 8; j++) sum[i][j] = sum[i - 1][j] + (j == x);
  }
  for (int i = n / 8; i >= 0; i--) {
    ans = Soviet(i);
    if (ans != -1) {
      printf("%d", ans);
      return 0;
    }
  }
}
