#include <bits/stdc++.h>
using namespace std;
long long exp(long long b, long long e) {
  if (b == 1 || e == 0) return 1;
  if (e == 1) return b % 1000000007;
  if (e == 2) return (b * b) % 1000000007;
  return ((e % 2 == 0)
              ? exp(exp(b, e / 2), 2) % 1000000007
              : (b * (exp(exp(b, e / 2), 2) % 1000000007)) % 1000000007);
}
long long gcd(long long a, long long b) {
  return ((b == 0) ? a : gcd(b, a % b));
}
int pw[10], a[1111], dp[1111][257], n;
vector<int> idx[9];
int possible(int pos, int mask, int cnt) {
  if (dp[pos][mask] != -1) return dp[pos][mask];
  if (pos >= n || mask == 255) {
    if (mask == 255 || cnt == 0) return (dp[pos][mask] = 0);
    return (dp[pos][mask] = -1);
  }
  int ret = -1;
  for (int i = 0; i < 8; ++i) {
    if (mask & pw[i]) continue;
    int s = -1, e = idx[i + 1].size(), mid, t;
    if (e == 0 && cnt > 0) return (dp[pos][mask] = -1);
    while (e - s > 1) {
      mid = (s + e) / 2;
      if (idx[i + 1][mid] < pos)
        s = mid;
      else
        e = mid;
    }
    if (e + cnt - 1 >= (int)idx[i + 1].size()) return (dp[pos][mask] = -1);
    if (e + cnt > 0) {
      t = possible(idx[i + 1][e + cnt - 1] + 1, mask | pw[i], cnt);
      if (t != -1) {
        ret = (((ret) > (cnt + t)) ? (ret) : (cnt + t));
      }
    } else {
      t = possible(pos, mask | pw[i], cnt);
      if (t != -1) {
        ret = (((ret) > (cnt + t)) ? (ret) : (cnt + t));
      }
    }
    if (e + cnt < idx[i + 1].size()) {
      t = possible(idx[i + 1][e + cnt] + 1, mask | pw[i], cnt);
      if (t != -1) {
        ret = (((ret) > (cnt + 1 + t)) ? (ret) : (cnt + 1 + t));
      }
    }
  }
  return (dp[pos][mask] = ret);
}
int check[10] = {0};
int main() {
  int i, j, k;
  pw[0] = 1;
  for (i = 1; i < 10; ++i) pw[i] = 2 * pw[i - 1];
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    idx[a[i]].push_back(i);
  }
  for (i = 0; i < n; ++i) {
    check[a[i]]++;
  }
  int ans = 0;
  int e = (((n) % (8) == 0) ? ((n) / (8)) : ((n) / (8) + 1)), s = -1, mid;
  while (e - s > 1) {
    for (i = 0; i <= n; ++i) {
      for (j = 0; j < 257; ++j) dp[i][j] = -1;
    }
    mid = (e + s) / 2;
    int t = possible(0, 0, mid);
    if (t > 0)
      s = mid;
    else
      e = mid;
    ans = (((ans) > (t)) ? (ans) : (t));
  }
  printf("%d\n", ans);
  return 0;
}
