#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const double eps = 1e-12;
template <typename T>
inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
const int N = 1010;
int n, a[N], ans = 0, dp[N][1 << 8], pos, mark, cnt;
vector<int> vec[8];
bool check(int len) {
  bool fl = 0;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < (1 << 8); j++) dp[i][j] = -INF;
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < (1 << 8); j++)
      if (dp[i][j] != -INF) {
        for (int k = 0; k < 8; k++)
          if (!(j & (1 << k))) {
            int L =
                upper_bound(vec[k].begin(), vec[k].end(), i) - vec[k].begin();
            int R = L + len - 1;
            if (R >= (int)vec[k].size()) continue;
            pos = vec[k][R];
            dp[pos][j | (1 << k)] = max(dp[pos][j | (1 << k)], dp[i][j] + len);
            ++R;
            if (R >= (int)vec[k].size()) continue;
            pos = vec[k][R];
            dp[pos][j | (1 << k)] =
                max(dp[pos][j | (1 << k)], dp[i][j] + len + 1);
          }
        if (j == (1 << 8) - 1) fl = 1, ans = max(ans, dp[i][j]);
      }
  return fl;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    vec[a[i] - 1].push_back(i);
    if (mark & (1 << (a[i] - 1)))
      ;
    else
      mark |= 1 << (a[i] - 1), cnt++;
  }
  if (mark == (1 << 8) - 1) {
    int l = 0, r = n / 8 + 1;
    while (l <= r) {
      int mid = (l + r) >> 1;
      if (check(mid))
        l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%d\n", ans);
  } else
    printf("%d\n", cnt);
}
