#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
int a[N], cnt[10], nxt[N][N][10];
int dp[N][1 << 10], n;
int can(int x) {
  memset(dp, -1, sizeof dp);
  memset(cnt, 0, sizeof cnt);
  for (int i = 1; i <= n; i++) {
    cnt[a[i]]++;
    for (int j = 1; j < (1 << 10); j++) {
      if (__builtin_popcount(j) != 1) continue;
      for (int k = 1; k <= 8; k++)
        if (j & (1 << k)) {
          if (cnt[k] == x) dp[i][j] = x;
          if (cnt[k] > x) dp[i][j] = x + 1;
        }
    }
  }
  for (int j = 1; j < (1 << 10); j++) {
    for (int i = 1; i < n; i++) {
      if (dp[i][j] == -1) continue;
      int how;
      for (int k = 1; k <= 8; k++) {
        how = x;
        if (j & (1 << k)) continue;
        if (how && how < N && nxt[i + 1][how][k] != n + 1)
          dp[nxt[i + 1][how][k]][j | (1 << k)] =
              max(dp[nxt[i + 1][how][k]][j | (1 << k)], dp[i][j] + how);
        how++;
        if (how && how < N && nxt[i + 1][how][k] != n + 1)
          dp[nxt[i + 1][how][k]][j | (1 << k)] =
              max(dp[nxt[i + 1][how][k]][j | (1 << k)], dp[i][j] + how);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < (1 << 10); j++) {
      if (__builtin_popcount(j) == 8) {
        ans = max(ans, dp[i][j]);
      } else {
        if (x == 0 && dp[i][j] == __builtin_popcount(j))
          ans = max(ans, dp[i][j]);
      }
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      for (int k = 0; k < 10; k++) nxt[i][j][k] = n + 1;
  for (int i = 0; i < 10; i++) cnt[i] = n + 1;
  for (int i = n; i >= 0; i--) {
    for (int k = 1; k <= 8; k++) {
      if (a[i] == k) nxt[i][0][k] = i;
    }
    for (int j = 1; j < N; j++) {
      for (int k = 1; k <= 8; k++) {
        if (a[i] == k) {
          if (j == 1)
            nxt[i][j][k] = i;
          else
            nxt[i][j][k] = nxt[cnt[k]][j - 1][k];
        } else
          nxt[i][j][k] = nxt[i + 1][j][k];
      }
    }
    cnt[a[i]] = i;
  }
  int st = 0, en = 1100;
  while (en - st > 1) {
    int mid = (st + en) / 2;
    if (can(mid))
      st = mid;
    else
      en = mid;
  }
  cout << can(st) << endl;
  return 0;
}
