#include <bits/stdc++.h>
using namespace std;
const int MAXC = 125;
const int MAXN = 1000;
int pre[8], last[MAXN + 10][MAXC + 5], f[MAXN + 10][(1 << 8) + 10],
    col[MAXN + 10];
int main() {
  int n, tmp, cmx;
  scanf("%d", &n);
  cmx = (n + 7) / 8;
  memset(last, -1, sizeof last);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &tmp);
    tmp--;
    col[i] = tmp;
    for (int c = 2; c <= cmx; c++) {
      last[i][c] = last[pre[tmp]][c - 1];
      if (last[i][c] == -1) break;
    }
    last[i][1] = i;
    pre[tmp] = i;
  }
  int ans = 0, mxS = (1 << 8);
  for (int i = 0; i < 8; i++)
    if (pre[i] != 0) ans++;
  for (int c = 2; c <= cmx; c++) {
    memset(f, -1, sizeof f);
    f[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      for (int S = 0; S < mxS; S++) f[i][S] = f[i - 1][S];
      int prep1 = last[i][c], prep2 = last[i][c - 1];
      for (int S = 0; S < mxS; S++) {
        if ((S & (1 << col[i])) > 0) {
          if (prep1 != -1) {
            if (f[prep1 - 1][S ^ (1 << col[i])] != -1)
              f[i][S] = max(f[i][S], f[prep1 - 1][S ^ (1 << col[i])] + c);
          }
          if (prep2 != -1) {
            if (f[prep2 - 1][S ^ (1 << col[i])] != -1)
              f[i][S] = max(f[i][S], f[prep2 - 1][S ^ (1 << col[i])] + c - 1);
          }
        }
      }
    }
    ans = max(ans, f[n][mxS - 1]);
  }
  printf("%d\n", ans);
  return 0;
}
