#include <bits/stdc++.h>
using namespace std;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int ara[1200], vis[12], n;
int dp[13][2000], cnt, st[1200][1260], up[13][1200], ck = 0;
int vr[1009][512];
int binary(int pos, int need) {
  int v = ara[pos], m, i, j, k, l, mid, ans = -1;
  m = dp[v][pos - 1];
  l = pos, k = n;
  while (l <= k) {
    mid = (l + k) / 2;
    if ((dp[v][mid] - m) < need)
      l = mid + 1;
    else if ((dp[v][mid] - m) > need)
      k = mid - 1;
    else if ((dp[v][mid] - m) == need) {
      ans = mid + 1;
      k = mid - 1;
    }
  }
  return ans;
}
int dp_func(int pos, int mask) {
  if (mask == (1 << 8) - 1) return 0;
  if (pos == n + 1) return -214748364;
  if (vr[pos][mask] != -1) return st[pos][mask];
  int ret = -214748364;
  if (check(mask, ara[pos]) == 0) {
    int l = binary(pos, cnt);
    if (l > -1) ret = dp_func(l, Set(mask, ara[pos])) + cnt;
    l = binary(pos, cnt + 1);
    if (l > -1) ret = max(ret, dp_func(l, Set(mask, ara[pos])) + cnt + 1);
  }
  ret = max(ret, dp_func(pos + 1, mask));
  vr[pos][mask] = 1;
  return st[pos][mask] = ret;
}
vector<int> vc;
int main() {
  int i, j, k, l, m;
  memset(dp, 0, sizeof dp);
  cin >> n;
  if (n == 999) ck = 1;
  memset(vis, 0, sizeof vis);
  for (i = 1; i <= n; i++) {
    cin >> ara[i];
    ara[i]--;
    if (vis[ara[i]] == 0) {
      vc.push_back(ara[i]);
      vis[ara[i]] = 1;
    }
  }
  for (i = 0; i < 8; i++) {
    if (vis[i] == 0) {
      cout << vc.size() << endl;
      return 0;
    }
  }
  for (i = 0; i < 8; i++) {
    dp[i][0] = 0;
    up[i][0] = -1;
    for (j = 1; j <= n; j++) {
      if (ara[j] == i) {
        dp[i][j] = dp[i][j - 1] + 1;
        up[i][j] = j;
      } else {
        dp[i][j] = dp[i][j - 1];
        up[i][j] = up[i][j - 1];
      }
    }
  }
  int ma = 0;
  for (i = 1; i <= 125; i++) {
    memset(vr, -1, sizeof vr);
    cnt = i;
    l = dp_func(1, 0);
    ma = max(ma, l);
  }
  cout << ma << endl;
}
