#include <bits/stdc++.h>
using namespace std;
int n;
int v[1100];
int pd[1100][1 << 8];
int qtd1, qtd2;
int vis[1100][1 << 8];
int inv[1100];
vector<int> pos[10];
bool check(int mask) {
  for (int i = 0; i < 8; ++i) {
    if (mask & (1 << i)) continue;
    if (qtd1) return false;
  }
  return true;
}
int f(int x, int mask) {
  if (x >= n) {
    if (check(mask))
      return 0;
    else
      return -0x3F3F3F3F;
  }
  int &p = pd[x][mask];
  if (vis[x][mask] < qtd2) {
    vis[x][mask] = qtd2;
    p = f(x + 1, mask);
    if (!(mask & (1 << v[x]))) {
      int fim, qtd;
      qtd = pos[v[x]].size() - inv[x];
      if (qtd1 && qtd >= qtd1) {
        fim = pos[v[x]][inv[x] + qtd1 - 1] + 1;
        p = max(p, f(fim, mask | (1 << v[x])) + qtd1);
      }
      qtd = pos[v[x]].size() - inv[x];
      if (qtd >= qtd2) {
        fim = pos[v[x]][inv[x] + qtd2 - 1] + 1;
        p = max(p, f(fim, mask | (1 << v[x])) + qtd2);
      }
    }
  }
  return p;
}
int main() {
  int ans = 1;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &v[i]);
    v[i]--;
    inv[i] = (int)pos[v[i]].size();
    pos[v[i]].push_back(i);
  }
  for (int i = 0; i < n; ++i) {
    qtd1 = i, qtd2 = i + 1;
    ans = max(ans, f(0, 0));
  }
  printf("%d\n", ans);
  return 0;
}
