#include <bits/stdc++.h>
using namespace std;
int a[100050];
int ans, n;
vector<int> mp[1050];
int cur[1050];
int dp[1050][1 << 8];
int Slove(int mid) {
  int end = (1 << 8);
  for (int i = 1; i <= 8; i++) cur[i] = 0;
  memset(dp, -1, sizeof(dp));
  dp[1][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < end; j++) {
      if (dp[i][j] < 0) continue;
      for (int k = 1; k <= 8; k++) {
        if (j & (1 << (k - 1))) continue;
        if (mp[k].size() - cur[k] < mid - 1) continue;
        dp[mp[k][cur[k] + mid - 2]][j | (1 << (k - 1))] =
            max(dp[mp[k][cur[k] + mid - 2]][j + (1 << (k - 1))], dp[i][j]);
        if (mp[k].size() - cur[k] < mid) continue;
        dp[mp[k][cur[k] + mid - 1]][j | (1 << (k - 1))] =
            max(dp[mp[k][cur[k] + mid - 1]][j + (1 << (k - 1))], dp[i][j] + 1);
      }
    }
    cur[a[i]]++;
  }
  int sum = -0x3f3f3f3f;
  for (int i = 1; i <= n; i++) sum = max(sum, dp[i][(1 << 8) - 1]);
  if (sum <= 0) return 0;
  ans = max(ans, sum * mid + (8 - sum) * (mid - 1));
  return 1;
}
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) mp[a[i]].push_back(i);
    int l = 2, r = n;
    ans = 0;
    while (r - l >= 0) {
      int mid = (l + r) / 2;
      if (Slove(mid) == 1) {
        l = mid + 1;
      } else
        r = mid - 1;
    }
    if (ans == 0) {
      for (int i = 1; i <= 8; i++)
        if (mp[i].size() > 0) ans++;
    }
    printf("%d\n", ans);
  }
}
