#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
const int INF = 0x3f3f3f3f;
int n, a[maxn];
vector<int> vec[8];
unordered_map<int, int> M;
int freq;
int dp[maxn][1 << 8];
int func(int i, int mask) {
  if (dp[i][mask] == -1) {
    if (i > n) {
      if (!freq) return dp[i][mask] = 0;
      if (mask) return dp[i][mask] = -INF;
      return dp[i][mask] = 0;
    }
    dp[i][mask] = func(i + 1, mask);
    if ((1 << a[i]) & mask) {
      int x = M[i];
      int y = x + freq - 1;
      if (y >= x && y < vec[a[i]].size())
        dp[i][mask] =
            max(dp[i][mask], freq + func(vec[a[i]][y] + 1, mask - (1 << a[i])));
      y = x + freq;
      if (y >= x && y < vec[a[i]].size())
        dp[i][mask] = max(
            dp[i][mask], freq + 1 + func(vec[a[i]][y] + 1, mask - (1 << a[i])));
    }
  }
  return dp[i][mask];
}
int solve() {
  int ans = 0;
  for (int f = 0; f <= n; ++f) {
    memset(dp, -1, sizeof(dp));
    freq = f;
    int len = func(1, (1 << 8) - 1);
    if (len == -INF) break;
    ans = max(ans, len);
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    --a[i];
    vec[a[i]].push_back(i);
    M[i] = vec[a[i]].size() - 1;
  }
  printf("%d\n", solve());
  return 0;
}
