#include <bits/stdc++.h>
using namespace std;
vector<int> g[10];
int a[1010], p[10], cur[10], dp[1010][1 << 8];
int n;
int check(int len) {
  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] == -1) continue;
      for (int k = 1; k <= 8; ++k) {
        if (j & (1 << (k - 1))) continue;
        int x = cur[k] + len - 1;
        if (x >= g[k].size()) continue;
        dp[g[k][x]][j | (1 << (k - 1))] =
            max(dp[g[k][x]][j | (1 << (k - 1))], dp[i][j]);
        ++x;
        if (x >= g[k].size()) continue;
        dp[g[k][x]][j | (1 << (k - 1))] =
            max(dp[g[k][x]][j | (1 << (k - 1))], dp[i][j] + 1);
      }
    }
    ++cur[a[i]];
  }
  int ans = -1;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, dp[i][(1 << 8) - 1]);
  }
  if (ans == -1) return -1;
  return ans * (len + 1) + (8 - ans) * len;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    g[a[i]].push_back(i);
  }
  int s = 1, t = n / 8, mid;
  while (s + 1 < t) {
    mid = s + t >> 1;
    if (check(mid) != -1)
      s = mid;
    else
      t = mid - 1;
  }
  int ans = max(check(s), check(t));
  if (ans == -1) {
    ans = 0;
    for (int i = 1; i <= 8; ++i)
      if (g[i].size()) ++ans;
  }
  printf("%d\n", ans);
  return 0;
}
