#include <bits/stdc++.h>
using namespace std;
int n;
const int ALL_SET = (1 << 8) - 1;
int dp[1005][(1 << 8)];
int arr[1004];
int solve(int ind, int mask, int c) {
  if (ind == n) {
    if (mask == ALL_SET || c == 1)
      return 0;
    else
      return -1e6;
  }
  if (dp[ind][mask] != -1) return dp[ind][mask];
  int ans = solve(ind + 1, mask, c);
  int x = (1 << (arr[ind] - 1));
  if ((x & mask) == 0) {
    int c2 = 0;
    for (int i = ind; i < n && c2 <= c; i++) {
      if (arr[i] == arr[ind]) {
        c2++;
        if (c2 == c - 1) {
          int mask1 = (mask | x);
          ans = max(ans, c2 + solve(i + 1, mask1, c));
        }
        if (c2 == c) {
          int mask1 = (mask | x);
          ans = max(ans, c + solve(i + 1, mask1, c));
        }
      }
    }
  }
  return dp[ind][mask] = ans;
}
int bin_search() {
  int l = 1, h = n / 8 + 2, mid;
  while (l < h) {
    mid = l + (h - l + 1) / 2;
    memset(dp, -1, sizeof(dp));
    int ans = solve(0, 0, mid);
    if (ans > 0)
      l = mid;
    else
      h = mid - 1;
  }
  memset(dp, -1, sizeof(dp));
  int ans = solve(0, 0, l);
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
  printf("%d\n", bin_search());
  return 0;
}
