#include <bits/stdc++.h>
using namespace std;
int N;
int A[1010];
int nex[1010][8][130];
int V[10];
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N;
  for (i = 0; i < (N); i++) cin >> A[i + 1], A[i + 1]--;
  for (i = 0; i < (8); i++) nex[N][i][1] = nex[N + 1][i][1] = N + 1;
  for (i = 0; i < (8); i++) nex[N][i][0] = N, nex[N + 1][i][0] = N + 1;
  for (i = N - 1; i >= 0; i--) {
    for (x = 0; x < (8); x++) {
      nex[i][x][0] = i;
      if (A[i + 1] == x)
        nex[i][x][1] = i + 1;
      else
        nex[i][x][1] = nex[i + 1][x][1];
    }
  }
  for (i = 2; i <= 128; i++) {
    for (x = 0; x < (N + 2); x++) {
      for (y = 0; y < (8); y++) nex[x][y][i] = nex[nex[x][y][i - 1]][y][1];
    }
  }
  int ma = 0;
  for (i = 0; i < (8); i++) V[i] = i;
  do {
    for (i = 125; i >= 1; i--) {
      x = 0;
      for (y = 0; y < (8); y++) x = nex[x][V[y]][i];
      if (x <= N) break;
    }
    int len = i, mask;
    ma = max(ma, len * 8);
    for (mask = 0; mask < (256); mask++) {
      x = 0;
      for (y = 0; y < (8); y++) x = nex[x][V[y]][len + ((mask & (1 << y)) > 0)];
      if (x <= N) ma = max(ma, len * 8 + __builtin_popcount(mask));
    }
  } while (next_permutation(V, V + 8));
  cout << ma << endl;
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  solve();
  return 0;
}
