#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n;
int dp[maxn][256], a[maxn], cur[8];
vector<int> pos[maxn];
int ok(int len) {
  int ans = -1;
  memset(dp, -1, sizeof(dp));
  memset(cur, 0, sizeof(cur));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 256; j++) {
      if (dp[i][j] == -1) continue;
      for (int k = 0; k < 8; k++) {
        if (j & (1 << k)) continue;
        int p = cur[k] + len - 1;
        if (pos[k].size() <= p) continue;
        dp[pos[k][p] + 1][j | 1 << k] =
            max(dp[pos[k][p]][j | 1 << k], dp[i][j]);
        if (pos[k].size() <= p + 1) continue;
        dp[pos[k][p + 1] + 1][j | 1 << k] =
            max(dp[pos[k][p + 1] + 1][j | 1 << k], dp[i][j] + 1);
      }
    }
    cur[a[i]]++;
  }
  for (int i = 0; i < n + 1; i++) ans = max(ans, dp[i][255]);
  if (ans == -1) return ans;
  return ans * (len + 1) + (8 - ans) * len;
}
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
int main() {
  n = read();
  for (int i = 0; i < n; i++) a[i] = read() - 1;
  for (int i = 0; i < n; i++) pos[a[i]].push_back(i);
  int l = 1, r = n / 8;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (ok(mid) != -1)
      l = mid;
    else
      r = mid;
  }
  int ans = max(ok(l), ok(r));
  if (ans == -1) {
    ans = 0;
    for (int i = 0; i < 8; i++)
      if (pos[i].size()) ans++;
  }
  printf("%d\n", ans);
  return 0;
}
