#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N];
int n;
int p[8] = {1, 2, 3, 4, 5, 6, 7, 8};
vector<vector<int>> v;
bool b[99];
int bc[999];
bool check(int k) {
  if (k == 0) return true;
  int idx = 0, cnt = 0;
  for (int i = 0; i < n; ++i)
    if (a[i] == p[idx]) {
      if (++cnt == k) {
        cnt = 0;
        if (++idx == 8) return true;
      }
    }
  return false;
}
bool checkv(vector<int>& v, int msk, int k) {
  if (k == 0) return true;
  int idx = 0, cnt = 0;
  for (int i = 0; i < n; ++i)
    if (a[i] == v[idx]) {
      if (++cnt == k + (msk >> idx & 1)) {
        cnt = 0;
        if (++idx == 8) return true;
      }
    }
  return false;
}
void add() {
  v.emplace_back();
  for (int i = 0; i < 8; ++i) v.back().push_back(p[i]);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  int m = 0;
  do {
    int l = 0, r = n / 8;
    while (l < r) {
      int t = (l + r + 1) / 2;
      if (check(t))
        l = t;
      else
        r = t - 1;
    }
    if (l > m) {
      v.clear();
      m = l;
    }
    if (m == l && m > 0) add();
  } while (next_permutation(p, p + 8));
  if (m == 0) {
    for (int i = 0; i < n; ++i) b[a[i]] = true;
    int k = 0;
    for (int i = 1; i <= 8; ++i) k += b[i];
    printf("%d\n", k);
    return 0;
  }
  int k = 0;
  for (int i = 0; i < 256; ++i) bc[i] = __builtin_popcount(i);
  for (auto& x : v) {
    for (int msk = 0; msk < 256; ++msk) {
      if (checkv(x, msk, m)) k = max(k, bc[msk]);
    }
  }
  m = m * 8 + k;
  printf("%d\n", m);
  return 0;
}
