#include <bits/stdc++.h>
using namespace std;
int n, dp[1010][1 << 8];
int a[1010];
int N[1010][1010][10];
int getbit(int n, int x) { return (n >> (x - 1)) & 1; }
void maxx(int &a, int b, int tmp) {
  if (b > -1 && a < b + tmp) a = b + tmp;
}
int check(int num) {
  memset(dp, -1, sizeof(dp));
  dp[n + 1][0] = 0;
  for (int i = n; i >= 1; --i) {
    for (int bitmask = 0; bitmask < (1 << 8); ++bitmask) {
      dp[i][bitmask] = dp[i + 1][bitmask];
      for (int j = 1; j <= 8; ++j) {
        if (getbit(bitmask, j)) {
          int new_bit = bitmask ^ (1 << (j - 1));
          int pos1 = N[i][num][j];
          int pos2 = N[i][num + 1][j];
          if (pos1 <= n) maxx(dp[i][bitmask], dp[pos1 + 1][new_bit], num);
          if (pos2 <= n) maxx(dp[i][bitmask], dp[pos2 + 1][new_bit], num + 1);
        }
      }
    }
  }
  return (dp[1][255] > -1);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n + 1; ++i)
    for (int j = 1; j <= n; ++j)
      for (int k = 1; k <= 8; ++k) N[i][j][k] = n + 1;
  for (int i = n; i >= 1; --i)
    for (int k = 1; k <= 8; ++k) N[i][1][k] = a[i] == k ? i : N[i + 1][1][k];
  for (int c = 1; c <= 8; ++c)
    for (int j = 2; j <= n; ++j)
      for (int i = 1; i <= n; ++i) {
        int tmp = N[i][j - 1][c];
        if (tmp <= n) N[i][j][c] = N[tmp + 1][1][c];
      }
  int lo, hi, mid;
  lo = 0;
  hi = n / 8;
  int ans = 0;
  while (lo <= hi) {
    mid = (lo + hi) >> 1;
    if (check(mid))
      ans = dp[1][255], lo = mid + 1;
    else
      hi = mid - 1;
  }
  cout << ans;
}
