#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10;
const int MN = 1e3 + 10;
int n, a[MN], ans;
int dp[256][MN], pre[8][MN];
int gc[8][MN], tgc[8][MN];
void Hehe(int w, int mid) {
  int cur = 0;
  for (int i = 1; i <= n; ++i)
    if (pre[w][i] >= mid) {
      while (cur <= i && pre[w][i] - pre[w][cur] >= mid) ++cur;
      gc[w][i] = cur - 1;
    }
  cur = 0;
  for (int i = 1; i <= n; ++i)
    if (pre[w][i] >= mid + 1) {
      while (cur <= i && pre[w][i] - pre[w][cur] >= mid + 1) ++cur;
      tgc[w][i] = cur - 1;
    }
}
bool check(int mid) {
  memset(gc, -1, sizeof(gc)), memset(tgc, -1, sizeof(tgc));
  for (int w = 0; w < 8; ++w) Hehe(w, mid);
  memset(dp, -10, sizeof(dp));
  dp[0][0] = 0;
  if (!mid)
    for (int i = 1; i < 256; ++i) dp[i][0] = 0;
  for (int i = 1; i <= n; ++i) {
    dp[0][i] = 0;
    for (int msk = 1; msk < 256; msk++) {
      int t = __builtin_popcount(msk);
      if (t * mid > i) continue;
      for (int w = 0; w < 8; ++w)
        if (msk >> w & 1) {
          if (~gc[w][i])
            dp[msk][i] = max(dp[msk][i], mid + dp[msk ^ 1 << w][gc[w][i]]);
          if (~tgc[w][i])
            dp[msk][i] = max(dp[msk][i], mid + 1 + dp[msk ^ 1 << w][tgc[w][i]]);
        }
    }
  }
  ans = max(ans, dp[(1 << 8) - 1][n]);
  return dp[(1 << 8) - 1][n] >= 0;
}
void prep() {
  for (int w = 0; w < 8; ++w)
    for (int i = 0; i < n; ++i) pre[w][i + 1] = pre[w][i] + (a[i] == w);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i], a[i]--;
  prep();
  int L = -1, R = n / 8 + 1;
  while (R - L > 1) {
    int mid = L + R >> 1;
    if (check(mid))
      L = mid;
    else
      R = mid;
  }
  cout << ans << endl;
  return 0;
}
