#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int dp[N][N], a[N];
int passed[N], n;
vector<int> pos[9];
int work(int len) {
  memset(dp, -1, sizeof(dp));
  memset(passed, 0, sizeof(passed));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << 8); j++) {
      if (dp[i][j] == -1) continue;
      for (int k = 0; k < 8; k++) {
        if (j & (1 << k)) continue;
        int posLen = passed[k + 1] + len - 1;
        if (posLen >= pos[k + 1].size()) continue;
        dp[pos[k + 1][posLen] + 1][j | (1 << k)] =
            max(dp[pos[k + 1][posLen] + 1][j | (1 << k)], dp[i][j] + len);
        posLen++;
        if (posLen >= pos[k + 1].size()) continue;
        dp[pos[k + 1][posLen] + 1][j | (1 << k)] =
            max(dp[pos[k + 1][posLen] + 1][j | (1 << k)], dp[i][j] + len + 1);
      }
    }
    passed[a[i]]++;
  }
  int result = -1;
  for (int i = 0; i <= n; i++) {
    result = max(result, dp[i][(1 << 8) - 1]);
  }
  return result;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    pos[a[i]].push_back(i);
  }
  int l = 1, r = n, last;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (work(mid) != -1) {
      last = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  int cnt = 0;
  for (int i = 0; i < 8; i++) cnt += pos[i + 1].size() != 0;
  cout << max(cnt, work(last)) << "\n";
  return 0;
}
