#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
int ans;
long long n = 1000000000000000000, m;
long long d;
int nint() {
  int x;
  scanf("%d", &x);
  return x;
}
int a[1050];
vector<int> v[10];
int g;
int table[300][1050];
int getans(int mask, int pos) {
  int &ans = table[mask][pos];
  if (ans != -1) return ans;
  ans = 0;
  for (int i = 0; i < (8); i++) {
    if (mask & (1 << i)) continue;
    for (int o = g; o <= g + 1; o++) {
      if (o == 0) {
        ans = max(ans, getans(mask | (1 << i), pos));
        continue;
      }
      int tmp = lower_bound((v[i]).begin(), (v[i]).end(), pos) - v[i].begin();
      tmp += o - 1;
      if (tmp < ((int)(v[i]).size()))
        ans = max(ans, o + getans(mask | (1 << i), v[i][tmp]));
    }
  }
  return ans;
}
int checklen(int len) {
  int p[8];
  for (int i = 0; i < (8); i++) p[i] = i;
  do {
    int pos = 0;
    int g = 1;
    for (int i = 0; i < (8); i++) {
      int x = p[i];
      int tmp = lower_bound((v[x]).begin(), (v[x]).end(), pos) - v[x].begin();
      tmp += len - 1;
      if (tmp >= ((int)(v[x]).size())) {
        g = 0;
        break;
      }
      pos = v[x][tmp];
    }
    if (g) return 1;
  } while (next_permutation(p, p + 8));
  return 0;
}
int main() {
  cin >> n;
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    a[i]--;
    v[a[i]].push_back(i);
  }
  int l = 1, r = 1000;
  int ans = 0;
  while (l <= r) {
    int c = (l + r) / 2;
    if (checklen(c)) {
      ans = c;
      l = c + 1;
    } else
      r = c - 1;
  }
  g = ans;
  memset(table, -1, sizeof(table));
  cout << getans(0, 0) << endl;
}
