#include <bits/stdc++.h>
using namespace std;
int n, ans, minFreq;
int a[1005];
int nxtPos[10][1005][1005];
int dp[1005][260];
int solve(int cur, int mask) {
  if (mask == 255) return 0;
  if (cur > n) return -1e9;
  if (dp[cur][mask] != -1) return dp[cur][mask];
  int r = -1e9;
  if (mask & (1 << (a[cur] - 1)))
    r = max(r, solve(cur + 1, mask));
  else {
    r = max(r, solve(cur + 1, mask));
    if (nxtPos[a[cur]][cur][minFreq])
      r = max(r, minFreq + solve(nxtPos[a[cur]][cur][minFreq] + 1,
                                 mask | (1 << (a[cur] - 1))));
    if (nxtPos[a[cur]][cur][minFreq + 1])
      r = max(r, minFreq + 1 +
                     solve(nxtPos[a[cur]][cur][minFreq + 1] + 1,
                           mask | (1 << (a[cur] - 1))));
  }
  return dp[cur][mask] = r;
}
int main() {
  set<int> s;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf(" %d", a + i), s.insert(a[i]);
  ans = (int)s.size();
  for (int k = 1; k <= 8; k++) {
    for (int i = 1; i <= n; i++) {
      int x = 0;
      for (int j = i; j <= n; j++) {
        if (a[j] == k) {
          x++;
          nxtPos[k][i][x] = j;
        }
      }
    }
  }
  for (minFreq = 0; minFreq <= n; minFreq++) {
    memset(dp, -1, sizeof(dp));
    ans = max(ans, solve(1, 0));
  }
  printf("%d\n", ans);
  return 0;
}
