#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, a[1010], dp[260][1010], lb = 0, ub = 1e9, mid, ans = 0;
bool ret;
vector<int> poss[10];
bool check() {
  for (int i = 0; i < 258; i++)
    for (int j = 0; j < 1005; j++) dp[i][j] = -1;
  dp[0][0] = 0;
  for (int i = 0; i < 256; i++) {
    for (int j = 0; j < n; j++) {
      if (dp[i][j] == -1) continue;
      for (int k = 0; k < 8; k++) {
        if (((i >> k) & 1) == 1) continue;
        int pos =
            lower_bound(poss[k].begin(), poss[k].end(), j) - poss[k].begin();
        if (pos + mid - 1 >= (int)(poss[k].size())) continue;
        if (mid == 0)
          dp[i | (1 << k)][j] = max(dp[i | (1 << k)][j], dp[i][j]);
        else
          dp[i | (1 << k)][poss[k][pos + mid - 1] + 1] =
              max(dp[i | (1 << k)][poss[k][pos + mid - 1] + 1], dp[i][j] + mid);
        if (pos + mid >= poss[k].size()) continue;
        dp[i | (1 << k)][poss[k][pos + mid] + 1] =
            max(dp[i | (1 << k)][poss[k][pos + mid] + 1], dp[i][j] + mid + 1);
      }
    }
  }
  ret = false;
  for (int i = 1; i <= n; i++) {
    if (dp[255][i] != -1) {
      ret = true;
      ans = max(ans, dp[255][i]);
    }
  }
  return ret;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    poss[a[i] - 1].push_back(i);
  }
  for (int i = 0; i < 8; i++) ub = min(ub, int(poss[i].size()));
  while (lb < ub) {
    mid = (lb + ub + 1) / 2;
    if (check())
      lb = mid;
    else
      ub = mid - 1;
  }
  mid = lb;
  check();
  cout << ans << endl;
  return 0;
}
