#include <bits/stdc++.h>
using namespace std;
const int N = 1010, K = 8, inF = N * N;
int n, a[N], go[N][K][2], dp[N][1 << K], ps[N][K];
bool one(int num, int dex) { return (num & (1 << dex)) > 0; }
int get(int num, int r, int want) {
  int s = 0, e = r + 1;
  while (e - s > 1) {
    int c = (s + e) >> 1;
    if (ps[r][num] - ps[c][num] >= want)
      s = c;
    else
      e = c;
  }
  if (ps[r][num] - ps[s][num] == want)
    return s;
  else
    return -1;
}
int check(int len) {
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < K; j++) {
      go[i][j][0] = get(j, i, len);
      go[i][j][1] = get(j, i, len + 1);
    }
  if (len)
    for (int mask = 1; mask < (1 << K); mask++) dp[0][mask] = -inF;
  for (int i = 1; i <= n; i++)
    for (int mask = 0; mask < (1 << K); mask++) {
      if (mask) dp[i][mask] = -inF;
      for (int bit = 0; bit < K; bit++)
        if (one(mask, bit)) {
          if (go[i][bit][0] != -1)
            dp[i][mask] =
                max(dp[i][mask], dp[go[i][bit][0]][mask ^ (1 << bit)] + len);
          if (go[i][bit][1] != -1)
            dp[i][mask] = max(dp[i][mask],
                              dp[go[i][bit][1]][mask ^ (1 << bit)] + len + 1);
        }
    }
  return dp[n][(1 << K) - 1];
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ps[i][--a[i]] = 1;
    for (int j = 0; j < K; j++) ps[i][j] += ps[i - 1][j];
  }
  int l = -1, r = n;
  while (r - l > 1) {
    int c = (l + r) / 2;
    if (check(c) > 0)
      l = c;
    else
      r = c;
  }
  cout << check(l);
  return 0;
}
