#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);
int n;
int a[2020];
int nxt[9][1010][1010];
int ans;
int X;
int dp[1 << 8][1010];
int calc(int mask, int ind) {
  if (mask == (1 << 8) - 1) return 0;
  int &res = dp[mask][ind];
  if (res != -1) return res;
  res = -inf;
  for (int i = 0; i < 8; i++) {
    if ((1 << i) & mask) continue;
    int pos1 = nxt[i][ind][X];
    if (pos1 != -1) {
      res = max(res, calc(mask | (1 << i), pos1 + 1));
    }
    int pos2 = nxt[i][ind][X + 1];
    if (pos2 != -1) {
      res = max(res, 1 + calc(mask | (1 << i), pos2 + 1));
    }
  }
  return res;
}
int calc() {
  memset(dp, -1, sizeof dp);
  return calc(0, 0);
}
bool can() {
  if (calc() < 0) return false;
  return true;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i]--;
  }
  set<int> second;
  for (int i = 0; i < n; i++) second.insert(a[i]);
  ans = second.size();
  for (int c = 0; c < 8; c++) {
    for (int k = 1; k <= n + 10; k++) {
      nxt[c][n][k] = -1;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int c = 0; c < 8; c++) {
      if (a[i] == c)
        nxt[c][i][1] = i;
      else
        nxt[c][i][1] = nxt[c][i + 1][1];
      for (int k = 2; k <= n + 10; k++) {
        if (a[i] == c)
          nxt[c][i][k] = nxt[c][i + 1][k - 1];
        else
          nxt[c][i][k] = nxt[c][i + 1][k];
      }
    }
  }
  int l = 1, r = n;
  int best = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    X = mid;
    if (can()) {
      best = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  if (best) {
    X = best;
    ans = 8 * X + calc();
  }
  printf("%d\n", ans);
  return 0;
}
