#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (auto &i : v) cin >> i, --i;
  vector<vector<int> > pos(8);
  for (int i = 0; i < n; ++i) pos[v[i]].push_back(i + 1);
  long long aue = 0;
  auto ok = [&](int x) {
    vector<int> ptr(8);
    vector<vector<long long> > dp(n + 1, vector<long long>(1 << 8, -1e18));
    dp[0][0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < 8; ++j) {
        while (ptr[j] < pos[j].size() && pos[j][ptr[j]] <= i) {
          ++ptr[j];
        }
      }
      for (int mask = 0; mask < (1 << 8); ++mask) {
        if (dp[i][mask] == -1e18) continue;
        for (int col = 0; col < 8; ++col)
          if ((mask >> col) & 1 ^ 1) {
            int cnt = pos[col].size() - ptr[col];
            if (cnt >= x) {
              int ni = pos[col][ptr[col] + x - 1];
              int nmask = mask | (1 << col);
              dp[ni][nmask] = max(dp[ni][nmask], dp[i][mask]);
            }
            if (cnt >= x + 1) {
              int ni = pos[col][ptr[col] + x];
              int nmask = mask | (1 << col);
              dp[ni][nmask] = max(dp[ni][nmask], dp[i][mask] + 1);
            }
          }
      }
    }
    long long res = -1e18;
    for (int i = 0; i <= n; ++i) {
      res = max(res, dp[i][(1 << 8) - 1]);
    }
    if (res == -1e18) return 0;
    aue = max(aue, res * (x + 1) + (8 - res) * x);
    return 1;
  };
  int l = 1, r = n / 8, mid;
  while (l <= r) {
    mid = l + r >> 1;
    ok(mid) ? l = mid + 1 : r = mid - 1;
  }
  long long kek = 0;
  for (int i = 0; i < 8; ++i) {
    if (pos[i].size()) ++kek;
  }
  aue = max(aue, kek);
  cout << aue;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q = 1;
  while (q--) solve();
}
