#include <bits/stdc++.h>
using namespace std;
int n, a[1010], dp[1010][260], g[10], f[10][1010], h[1010];
int check(int x) {
  memset(dp, -0x3f3f3f3f, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 256; j++) {
      if (i > 0 && dp[i - 1][j] != -0x3f3f3f3f) dp[i][j] = dp[i - 1][j];
      if (j & (1 << (a[i] - 1))) {
        if (h[i] >= x)
          dp[i][j] =
              max(dp[i][j], dp[f[a[i]][h[i] - x]][j ^ (1 << (a[i] - 1))] + x);
        if (h[i] >= x + 1)
          dp[i][j] =
              max(dp[i][j],
                  dp[f[a[i]][h[i] - x - 1]][j ^ (1 << (a[i] - 1))] + x + 1);
      }
    }
  return dp[n - 1][255];
}
int Sol() {
  int l = 1, r = n, mid, ans = 0;
  for (int i = 1; i <= 8; i++) {
    r = min(r, g[i] + 1);
    if (g[i] > 0) ans++;
  }
  while (l <= r) {
    mid = (l + r) >> 1;
    int x = check(mid);
    ans = max(ans, x);
    if (x > 0)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return ans;
}
int main() {
  memset(f, -1, sizeof(f));
  memset(g, 0, sizeof(g));
  memset(h, 0, sizeof(h));
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    f[a[i]][g[a[i]]++] = i;
    h[i] = g[a[i]];
  }
  printf("%d\n", Sol());
  return 0;
}
