#include <bits/stdc++.h>
using namespace std;
int cur[10];
int dp[1005][256];
vector<int> pos[10];
int a[1005];
int n;
int check(int len) {
  memset(dp, 0x80, sizeof(dp));
  memset(cur, 0, sizeof(cur));
  dp[1][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int m = 0; m < 256; m++) {
      if (dp[i][m] == 0x80808080) continue;
      for (int k = 1; k <= 8; k++) {
        if (m & (1 << k - 1)) continue;
        int newcur = cur[k] + len - 1;
        if (newcur >= pos[k].size()) continue;
        dp[pos[k][newcur] + 1][m | (1 << k - 1)] =
            max(dp[pos[k][newcur] + 1][m | (1 << k - 1)], dp[i][m]);
        newcur++;
        if (newcur >= pos[k].size()) continue;
        dp[pos[k][newcur] + 1][m | (1 << k - 1)] =
            max(dp[pos[k][newcur] + 1][m | (1 << k - 1)], dp[i][m] + 1);
      }
    }
    cur[a[i]]++;
  }
  int ret = 0x80808080;
  for (int i = 1; i <= n + 1; i++) ret = max(ret, dp[i][255]);
  if (ret == 0x80808080)
    return -1;
  else
    return ret * (len + 1) + (8 - ret) * len;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    pos[a[i]].push_back(i);
  }
  int l = 1, r = n / 8 + 1;
  while (l < r) {
    int mid = l + r >> 1;
    int ret = check(mid);
    if (ret == -1)
      r = mid;
    else
      l = mid + 1;
  }
  l--;
  if (l == 0) {
    int ans = 0;
    for (int i = 1; i <= 8; i++)
      if (pos[i].size()) ans++;
    printf("%d\n", ans);
  } else
    printf("%d\n", check(l));
  return 0;
}
