#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T, class X>
inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}
inline long long power(long long a, long long b) {
  long long multiply = 1;
  for (int i = (1); i <= (b); i++) {
    multiply *= a;
  }
  return multiply;
}
inline long long ABS(long long a) { return (a >= 0) ? a : -a; }
int n, arr[1005], dp[1005][(1 << 8)], lower, cnt[8], where[8][2007], come[1007];
int call(int pos, int mask) {
  if (__builtin_popcount(mask) == 8) return 0;
  if (pos == n + 1) return -(1 << 30) - 1;
  if (dp[pos][mask] != -1) return dp[pos][mask];
  int ret = -(1 << 30) - 1;
  ret = max(ret, call(pos + 1, mask));
  if (getbit(mask, arr[pos]) == 0) {
    if (where[arr[pos]][come[pos] + lower - 1] != -1)
      ret = max(ret, lower + call(where[arr[pos]][come[pos] + lower - 1] + 1,
                                  setbit(mask, arr[pos])));
    if (where[arr[pos]][come[pos] + lower] != -1)
      ret = max(ret, lower + 1 +
                         call(where[arr[pos]][come[pos] + lower] + 1,
                              setbit(mask, arr[pos])));
  }
  return dp[pos][mask] = ret;
}
int main() {
  scanf("%d", &n);
  memset(cnt, 0, sizeof(cnt));
  for (int i = (1); i <= (n); i++) {
    scanf("%d", &arr[i]);
    arr[i]--;
    cnt[arr[i]]++;
  }
  int temp = 0;
  int lo = 1, hi = (1 << 30) - 1, mid, best, ans = -(1 << 30) - 1, now;
  for (int i = (0); i <= (7); i++) temp += (cnt[i] > 0);
  for (int i = (0); i <= (7); i++) hi = min(hi, cnt[i]);
  for (int i = (0); i <= (7); i++) cnt[i] = 1;
  memset(where, -1, sizeof(where));
  for (int i = (1); i <= (n); i++) {
    where[arr[i]][cnt[arr[i]]] = i;
    come[i] = cnt[arr[i]];
    cnt[arr[i]]++;
  }
  while (hi >= lo) {
    mid = (hi + lo) / 2;
    lower = mid;
    memset(dp, -1, sizeof(dp));
    now = call(1, 0);
    ans = max(now, ans);
    if (now < 0)
      hi = mid - 1;
    else
      lo = mid + 1;
  }
  ans = max(ans, temp);
  printf("%d\n", ans);
  return 0;
}
