#include <bits/stdc++.h>
using namespace std;
const int maxn = 3000;
vector<int> mp[3000];
int a[maxn], vis[maxn];
int ans, n, cur[maxn];
int dp[maxn][maxn];
int vaild(int mid) {
  memset(cur, 0, sizeof(cur));
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (1 << 8); j++) {
      if (dp[i][j] < 0) continue;
      for (int k = 1; k <= 8; k++) {
        if (j & (1 << (k - 1))) continue;
        if (mp[k].size() - cur[k] >= mid)
          dp[mp[k][cur[k] + mid - 1]][j | (1 << (k - 1))] =
              max(dp[mp[k][cur[k] + mid - 1]][j | (1 << (k - 1))], dp[i][j]);
        if (mp[k].size() - cur[k] >= mid + 1)
          dp[mp[k][cur[k] + mid]][j | (1 << (k - 1))] =
              max(dp[mp[k][cur[k] + mid]][j | (1 << (k - 1))], dp[i][j] + 1);
      }
    }
    if (i != n) cur[a[i + 1]]++;
  }
  for (int i = 0; i <= n; i++) {
    if (dp[i][(1 << 8) - 1] == -1) continue;
    ans = max(
        ans, dp[i][(1 << 8) - 1] * (mid + 1) + (8 - dp[i][(1 << 8) - 1]) * mid);
  }
  for (int i = 0; i <= n; i++) {
    if (dp[i][(1 << 8) - 1] >= 0) return 1;
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    vis[a[i]] = 1;
    mp[a[i]].push_back(i);
  }
  for (int i = 1; i <= 8; i++) {
    if (vis[i]) ans++;
  }
  int l = 0, r = n + 1;
  while (r - l > 1) {
    int mid = (r + l) / 2;
    if (vaild(mid))
      l = mid;
    else
      r = mid;
  }
  printf("%d\n", ans);
}
