#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3;
int n, a[maxn + 5];
vector<int> pos[10];
int f[maxn + 5][1 << 8];
int nxt1[maxn + 5][9], nxt2[maxn + 5][9];
int check(int L) {
  int P[9] = {};
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 8; j++) {
      while (P[j] < pos[j].size() && pos[j][P[j]] < i) P[j]++;
      if (P[j] + L - 1 < pos[j].size())
        nxt1[i][j] = pos[j][P[j] + L - 1];
      else
        nxt1[i][j] = n + 1;
      if (P[j] + L < pos[j].size())
        nxt2[i][j] = pos[j][P[j] + L];
      else
        nxt2[i][j] = n + 1;
    }
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < (1 << 8); j++) {
      f[i][j] = -1e9;
    }
  }
  f[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int st = 0; st < (1 << 8); st++) {
      if (f[i][st] == -1e9) continue;
      for (int j = 0; j < 8; j++) {
        if (st >> j & 1) continue;
        int p1 = nxt1[i + 1][j], p2 = nxt2[i + 1][j];
        int np = st | (1 << j);
        f[p1][np] = max(f[p1][np], f[i][st]);
        f[p2][np] = max(f[p2][np], f[i][st] + 1);
      }
      f[i + 1][st] = max(f[i + 1][st], f[i][st]);
    }
  }
  int st = (1 << 8) - 1;
  if (f[n][st] == -1e9) {
    return -1;
  } else {
    return L * 8 + f[n][st];
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i]--, pos[a[i]].push_back(i);
  int ans = 0;
  for (int i = 0; i < 8; i++) {
    if (pos[i].size() > 0) ans++;
  }
  int l = 1, r = n, ansp = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    int ret = check(mid);
    if (ret != -1) {
      ans = ret;
      ansp = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  check(1);
  cout << ans << '\n';
  return 0;
}
