#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10, P = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1;
int a[maxn], b[10] = {1, 2, 3, 4, 5, 6, 7, 8, 0, 0}, n, dp[maxn][10][maxn],
             ans = 0;
int check(int u) {
  int x = 0;
  for (int i = 0; i < 256; i++) {
    int po = n - 1, res = 0;
    for (int j = 0; j < 8; j++) {
      if (po == -1) {
        res = 0;
        break;
      }
      if ((1 << j) & i)
        po = dp[po][b[j]][u + 1], res += u + 1;
      else
        po = dp[po][b[j]][u], res += u;
      if (po == -2) {
        res = 0;
        break;
      }
    }
    x = max(x, res);
  }
  return x;
}
void find_ans() {
  for (int i = 0; i < P; i++) {
    int low = -1, high = n, res = 0;
    while (high - low > 1) {
      int mid = (low + high) / 2;
      if (check(mid) > 0) {
        res = max(res, check(mid));
        low = mid;
      } else
        high = mid;
    }
    ans = max(ans, res);
    next_permutation(b, b + 8);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++)
    for (int j = 1; j <= 8; j++) dp[i][j][0] = i;
  for (int i = 1; i <= 8; i++)
    for (int j = 1; j <= n; j++) dp[0][i][j] = -2;
  dp[0][a[0]][1] = -1;
  for (int i = 1; i < n; i++) {
    for (int j = 1; j <= 8; j++) {
      for (int k = 1; k <= n; k++) {
        if (a[i] == j)
          dp[i][j][k] = dp[i - 1][j][k - 1];
        else
          dp[i][j][k] = dp[i - 1][j][k];
      }
    }
  }
  find_ans();
  cout << ans << endl;
  return 0;
}
