#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
struct state {
  bool qt[8];
  state() { memset(qt, 0, sizeof qt); }
};
int dp[N][2][2][2][2][2][2][2][2];
int v[N], pre[N];
vector<int> pos[8];
int n, k;
int go(int i, state cur) {
  if (i == n) {
    if (k == 0) return 0;
    bool ok = true;
    for (int j = 0; j < 8; j++) ok = ok && cur.qt[j];
    if (ok)
      return 0;
    else
      return -0x3f3f3f3f;
  }
  if (dp[i][cur.qt[0]][cur.qt[1]][cur.qt[2]][cur.qt[3]][cur.qt[4]][cur.qt[5]]
        [cur.qt[6]][cur.qt[7]] != -1) {
    return dp[i][cur.qt[0]][cur.qt[1]][cur.qt[2]][cur.qt[3]][cur.qt[4]]
             [cur.qt[5]][cur.qt[6]][cur.qt[7]];
  }
  if (cur.qt[v[i]])
    return dp[i][cur.qt[0]][cur.qt[1]][cur.qt[2]][cur.qt[3]][cur.qt[4]]
             [cur.qt[5]][cur.qt[6]][cur.qt[7]] = go(i + 1, cur);
  int ans = go(i + 1, cur);
  int left = pos[v[i]].size() - pre[i];
  if (left >= k && k > 0) {
    state nxt = cur;
    nxt.qt[v[i]] = true;
    ans = max(ans, k + go(pos[v[i]][pre[i] + k - 1] + 1, nxt));
  }
  if (left >= k + 1) {
    state nxt = cur;
    nxt.qt[v[i]] = true;
    ans = max(ans, k + 1 + go(pos[v[i]][pre[i] + k] + 1, nxt));
  }
  return dp[i][cur.qt[0]][cur.qt[1]][cur.qt[2]][cur.qt[3]][cur.qt[4]][cur.qt[5]]
           [cur.qt[6]][cur.qt[7]] = ans;
}
int main() {
  scanf("%d", &n);
  int s[8];
  memset(s, 0, sizeof s);
  for (int i = 0; i < n; i++) {
    scanf("%d", v + i);
    v[i]--;
    pos[v[i]].push_back(i);
    pre[i] = s[v[i]];
    s[v[i]]++;
  }
  int ans = 0;
  for (k = 0; 8 * k <= n; k++) {
    memset(dp, -1, sizeof dp);
    state init;
    ans = max(ans, go(0, init));
  }
  printf("%d\n", ans);
  return 0;
}
