#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline long long lin() {
  long long x;
  scanf("%lld", &x);
  return x;
}
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const int SZ = (1 << 8);
int dp[SZ], n;
vector<int> pos[10];
inline bool can(int cnt) {
  int extraCnt = cnt % 8;
  for (int extmask = 0; extmask < SZ; extmask++) {
    if (__builtin_popcount(extmask) != extraCnt) continue;
    dp[0] = 0;
    for (int mask = 1; mask < SZ; mask++) {
      dp[mask] = n + 5;
      for (int lst = 0; lst < 8; lst++) {
        if (mask >> lst & 1) {
          int premask = mask ^ (1 << lst);
          int start = dp[premask];
          int low = (int)(lower_bound(pos[lst].begin(), pos[lst].end(), start) -
                          pos[lst].begin());
          int need = cnt / 8 + bool(extmask >> lst & 1);
          if (!need)
            dp[mask] = min(dp[mask], dp[premask]);
          else if (low + need <= pos[lst].size()) {
            dp[mask] = min(dp[mask], pos[lst][low + need - 1] + 1);
          }
        }
      }
      if (mask == SZ - 1 && dp[mask] <= n) {
        return true;
      }
    }
  }
  return 0;
}
int32_t main() {
  n = in();
  for (int i = 0; i < n; i++) pos[in() - 1].push_back(i);
  int low = 0, hi = n + 1;
  while (low + 1 < hi) {
    int mid = (low + hi) >> 1;
    if (can(mid))
      low = mid;
    else
      hi = mid;
  }
  cout << low << endl;
}
