#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1111, m = 8, MOD = 1e9 + 7, sq = 333;
long long dp[MAXN][(1 << m)];
long long a[MAXN];
long long ans = 1, n;
long long nxt[MAXN][MAXN], nxt2[MAXN][MAXN];
bool check(long long x) {
  memset(dp, -63, sizeof dp);
  dp[n][0] = 0;
  for (long long i = n - 1; i > -1; i--) {
    dp[i][0] = 0;
    for (long long mask = 1; mask < (1 << m); mask++) {
      dp[i][mask] = dp[i + 1][mask];
      for (long long j = 0; j < m; j++) {
        if (mask & (1 << j)) {
          long long k = nxt2[i][j];
          if (k == -1) continue;
          long long w = nxt[k][x - 1];
          if (w == -1) continue;
          dp[i][mask] = max(dp[i][mask], x + dp[w + 1][mask ^ (1 << j)]);
          w = nxt[k][x];
          if (w == -1) continue;
          dp[i][mask] = max(dp[i][mask], x + 1 + dp[w + 1][mask ^ (1 << j)]);
        }
      }
    }
  }
  ans = max(ans, dp[0][(1 << m) - 1]);
  return dp[0][(1 << m) - 1] > 0;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  memset(nxt, -1, sizeof nxt);
  for (long long i = n - 1; i > -1; i--) {
    nxt[i][0] = i;
    for (long long j = i + 1; j < n; j++)
      if (a[i] == a[j]) {
        for (long long k = MAXN - 1; k; k--) nxt[i][k] = nxt[j][k - 1];
        break;
      }
  }
  memset(nxt2, -1, sizeof nxt2);
  for (long long i = n - 1; i > -1; i--) {
    for (long long j = 0; j < 8; j++) nxt2[i][j] = nxt2[i + 1][j];
    nxt2[i][a[i] - 1] = i;
  }
  long long k = 0;
  for (long long i = 0; i < 8; i++) k += (nxt2[0][i] > -1);
  ans = k;
  long long l = 0, r = n;
  while (r - l > 1) {
    long long mid = (r + l) / 2;
    if (check(mid))
      l = mid;
    else
      r = mid;
  }
  cout << ans;
}
