#include <bits/stdc++.h>
using namespace std;
int mod = 1000000007;
const int inf = 2147483647;
const long long LL_INF = 1234567890123456789ll;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T>
T GCD(T a, T b) {
  long long t;
  while (a) {
    t = a;
    a = b % a;
    b = t;
  }
  return b;
}
template <typename T>
string toString(T a) {
  return to_string(a);
}
template <typename T>
void toInt(string s, T& x) {
  stringstream str(s);
  str >> x;
}
inline int add(int x, int y) {
  x += y;
  if (x >= mod) x -= mod;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0) x += mod;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % mod; }
inline int powr(int a, long long b) {
  int x = 1 % mod;
  while (b) {
    if (b & 1) x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a) { return powr(a, mod - 2); }
const int N = 1e3 + 5;
int arr[N], n;
int pos[9][N][N], dp[N][(1 << 8)];
int len;
int fun(int ind, int mask) {
  if (ind > n) return -5 * N;
  if (ind == n || mask == (1 << 8) - 1) {
    if (mask == (1 << 8) - 1) return 0;
    return -5 * N;
  }
  if (dp[ind][mask] != -1) return dp[ind][mask];
  int ans = -inf;
  for (int i = 1; i <= 8; i++) {
    if (!((mask >> (i - 1)) & 1)) {
      ans = max(ans, len + fun(pos[i][ind][len] + 1, (mask | (1 << (i - 1)))));
      ans =
          max(ans,
              len + 1 + fun(pos[i][ind][len + 1] + 1, (mask | (1 << (i - 1)))));
    }
  }
  return dp[ind][mask] = ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int v = 1; v <= 8; v++) {
    for (int i = 0; i < n; i++) {
      for (int c = 0; c <= n + 1; c++) {
        pos[v][i][c] = N;
      }
      int cnt = 0;
      pos[v][i][0] = i - 1;
      for (int j = i; j < n; j++) {
        cnt += (arr[j] == v);
        if (pos[v][i][cnt] == N) pos[v][i][cnt] = j;
      }
    }
  }
  int ans = 1;
  int low = 0, high = n, mid;
  while (low <= high) {
    mid = (low + high) >> 1;
    len = mid;
    memset(dp, -1, sizeof(dp));
    int cur = fun(0, 0);
    if (cur > 0) {
      ans = cur;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  cout << ans << '\n';
  return 0;
}
