#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ii = pair<int, int>;
const int N = 1e3 + 10, inf = 1e9;
int a[N], b[8][N][N], mx = (1 << 8) - 1, n, curr, dp[260][N];
int f(int mask, int idx) {
  if (idx > n + 1)
    return -inf;
  else if (mask == mx)
    return 0;
  else if (idx > n)
    return -inf;
  else if (dp[mask][idx] != -1)
    return dp[mask][idx];
  dp[mask][idx] = -inf;
  for (int i = 0; i < 8; ++i) {
    if (mask & (1 << i)) continue;
    dp[mask][idx] =
        max({dp[mask][idx], curr + f(mask | (1 << i), b[i][idx][curr] + 1),
             curr + 1 + f(mask | (1 << i), b[i][idx][curr + 1] + 1)});
  }
  return dp[mask][idx];
}
bool solve(int mid) {
  curr = mid;
  memset(dp, -1, sizeof(dp));
  return f(0, 1) >= 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  int mask = 0;
  for (int i = 1; i < n + 1; ++i) cin >> a[i], --a[i], mask |= (1 << a[i]);
  for (int card = 0; card < 8; ++card)
    for (int i = 1; i < n + 1; ++i)
      for (int j = 0; j < n + 2; ++j) b[card][i][j] = N - 3;
  for (int card = 0; card < 8; ++card)
    for (int i = 1; i < n + 1; ++i) {
      int c = 0;
      for (int j = i; j < n + 1; ++j) {
        int cprev = c;
        c += (a[j] == card);
        if (c != cprev) {
          b[card][i][c] = j;
        }
      }
    }
  int lo = 1, hi = n, mid;
  while (lo <= hi) {
    mid = (lo + hi) >> 1;
    bool x = solve(mid), y = solve(mid + 1);
    if (x && !y)
      break;
    else if (x && y)
      lo = mid + 1;
    else
      hi = mid - 1;
  }
  memset(dp, -1, sizeof(dp));
  curr = mid;
  cout << max(f(0, 1), __builtin_popcount(mask)) << endl;
  return 0;
}
