#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1000000007;
inline string getstr(string &s, int l, int r) {
  string ret = "";
  for (int i = l; i <= r; i++) ret.push_back(s[i]);
  return ret;
}
int modpow(int x, int y, int md = mod) {
  if (y == 0) return 1;
  int ret = modpow(x, y >> 1, md);
  ret = (long long)ret * ret % md;
  if (y & 1) ret = (long long)ret * x % md;
  return ret;
}
int n;
int a[1005];
int dp[1005][1 << 8];
int nxt[1005][205];
void init() {
  memset(nxt, -1, sizeof nxt);
  for (int i = 1; i <= n; i++) {
    int cnt = 0;
    for (int j = i; j <= n; j++) {
      if (a[i] == a[j]) {
        nxt[i][++cnt] = j;
      }
    }
  }
}
inline void upd(int &x, int y) { x = max(x, y); }
int check(int low) {
  memset(dp, -1, sizeof dp);
  dp[1][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int mask = 0; mask < (1 << 8); mask++) {
      if (dp[i][mask] == -1) continue;
      upd(dp[i + 1][mask], dp[i][mask]);
      if (mask & (1 << a[i])) continue;
      if (~nxt[i][low])
        upd(dp[nxt[i][low] + 1][mask | (1 << a[i])], dp[i][mask] + low);
      if (~nxt[i][low + 1])
        upd(dp[nxt[i][low + 1] + 1][mask | (1 << a[i])], dp[i][mask] + low + 1);
    }
  }
  return dp[n + 1][(1 << 8) - 1];
}
int main() {
  cin >> n;
  set<int> s;
  for (int i = 1; i <= n; i++) cin >> a[i], a[i]--, s.insert(a[i]);
  init();
  int l = 0, r = 200;
  while (r - l > 1) {
    int mid = l + r >> 1;
    if (~check(mid))
      l = mid;
    else
      r = mid;
  }
  if (l == 0) {
    cout << s.size() << endl;
    return 0;
  }
  cout << check(l) << endl;
  return 0;
}
