#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
int dp[MAXN][1 << 8];
int N, A[MAXN], pos[MAXN], sz;
vector<int> nums[10];
int f(int i, int mask) {
  if (i == N) return mask == 0 || sz == 0 ? 0 : -2;
  if (dp[i][mask] != -1) return dp[i][mask];
  dp[i][mask] = f(i + 1, mask);
  int n = A[i];
  if ((mask & (1 << (n - 1))) > 0) {
    if (pos[i] + sz - 1 < nums[n].size() && sz > 0) {
      int nxt = nums[n][pos[i] + sz - 1] + 1;
      if (f(nxt, mask & (~(1 << (n - 1)))) != -2) {
        dp[i][mask] = max(dp[i][mask], f(nxt, mask & (~(1 << (n - 1)))));
      }
    }
    if (pos[i] + sz < nums[n].size()) {
      int nxt = nums[n][pos[i] + sz] + 1;
      if (f(nxt, mask & (~(1 << (n - 1)))) != -2) {
        dp[i][mask] = max(dp[i][mask], f(nxt, mask & (~(1 << (n - 1)))) + 1);
      }
    }
  }
  return dp[i][mask];
}
int main() {
  scanf("%d", &N);
  for (int i = 0; i < (int)N; i++) {
    scanf("%d", &A[i]);
    pos[i] = nums[A[i]].size();
    nums[A[i]].push_back(i);
  }
  int lo = 0, hi = 200, ans;
  while (lo < hi) {
    sz = (lo + hi + 1) / 2;
    memset(dp, -1, sizeof(dp));
    ans = f(0, (1 << 8) - 1);
    if (ans == -2)
      hi = sz - 1;
    else
      lo = sz;
  }
  sz = lo;
  memset(dp, -1, sizeof(dp));
  ans = f(0, (1 << 8) - 1);
  printf("%d\n", 8 * sz + ans);
}
