#include <bits/stdc++.h>
using namespace std;
int n, x, cnt[1005][10], res = 8;
int dp[1005][(1 << 8) + 1];
void solve() {
  for (int len = 1; len <= n / 8; len++) {
    memset(dp, -1, sizeof(dp));
    dp[0][0] = 0;
    for (int pos = 0; pos < n; pos++) {
      for (int mask = 0; mask < (1 << 8); mask++) {
        if (dp[pos][mask] == -1) continue;
        for (int last = 0; last < 8; last++) {
          if (mask & (1 << last)) continue;
          int l, r, mid;
          l = pos + 1, r = n;
          if (cnt[n][last + 1] - cnt[pos][last + 1] < len) break;
          while (l < r) {
            mid = (l + r) >> 1;
            if (cnt[mid][last + 1] - cnt[pos][last + 1] < len)
              l = mid + 1;
            else
              r = mid;
          }
          dp[l][mask | (1 << last)] =
              max(dp[l][mask | (1 << last)], dp[pos][mask] + len);
          if (cnt[n][last + 1] - cnt[pos][last + 1] <= len) continue;
          l = pos + 1, r = n;
          while (l < r) {
            mid = (l + r) >> 1;
            if (cnt[mid][last + 1] - cnt[pos][last + 1] < len + 1)
              l = mid + 1;
            else
              r = mid;
          }
          dp[l][mask | (1 << last)] =
              max(dp[l][mask | (1 << last)], dp[pos][mask] + len + 1);
        }
      }
    }
    for (int i = 8; i <= n; i++) {
      res = max(res, dp[i][(1 << 8) - 1]);
    }
  }
  cout << res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    for (int j = 1; j <= 8; j++) cnt[i][j] = cnt[i - 1][j];
    cnt[i][x]++;
  }
  int ret = 0;
  for (int i = 1; i <= 8; i++) ret += bool(cnt[n][i]);
  if (ret < 8)
    cout << ret;
  else
    solve();
  return 0;
}
