#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, ans;
int a[N], dp[N][N][8];
bool f(int d) {
  bool res = false;
  vector<int> perm = {1, 2, 3, 4, 5, 6, 7, 8};
  do {
    for (int mask = 0; mask < (1 << 8); ++mask) {
      bool flag = true;
      int j = 0;
      for (auto x : perm) {
        if (j >= n) {
          flag = false;
          break;
        }
        j = dp[j][d + ((mask & (1 << (x - 1))) != 0)][x] + 1;
        if (j > n) {
          flag = false;
          break;
        }
      }
      if (flag) {
        res = true;
        int cur = d * 8;
        for (int i = 0; i < 8; ++i) cur += (mask & (1 << i)) != 0;
        ans = max(ans, cur);
      }
    }
  } while (next_permutation(perm.begin(), perm.end()));
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", a + i);
  for (int x = 1; x <= 8; ++x) {
    int cur = n;
    for (int i = n - 1; i >= 0; --i) {
      if (a[i] == x) cur = i;
      dp[i][1][x] = cur;
      dp[i][0][x] = i - 1;
    }
  }
  for (int x = 1; x <= 8; ++x) {
    for (int k = 2; k <= n; ++k) {
      for (int i = n - 1; i >= 0; --i) {
        int pos = dp[i][k - 1][x] + 1;
        if (pos >= n)
          dp[i][k][x] = n;
        else
          dp[i][k][x] = dp[pos][1][x];
      }
    }
  }
  int L = -1, R = n;
  while (R - L > 1) {
    int M = (L + R) >> 1;
    if (f(M))
      L = M;
    else
      R = M;
  }
  cout << ans << '\n';
  return 0;
}
