#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e3 + 9;
int n, a[MAX], ans;
int b[8] = {0, 1, 2, 3, 4, 5, 6, 7}, cnt[8], nxt[8][MAX][MAX], s[1 << 8];
bool check(int l, int m) {
  for (int i = 0; i < 8; i++) cnt[i] = l + (m >> i & 1);
  int p = 0;
  for (int i = 0; i < 8; i++)
    if (cnt[b[i]] != 0) {
      p = nxt[b[i]][p][cnt[b[i]]];
      if (p == -1) return false;
      p++;
    }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  memset(nxt, -1, sizeof nxt);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
  for (int i = 0; i < 8; i++)
    for (int j = n - 1; j >= 0; j--) {
      for (int k = j; k < n; k++)
        if (a[k] == i) {
          nxt[i][j][1] = k;
          break;
        }
      if (nxt[i][j][1] != -1)
        for (int k = 2; k <= n; k++)
          nxt[i][j][k] = nxt[i][nxt[i][j][1] + 1][k - 1];
    }
  for (int i = (1 << 8) - 1; i >= 0; i--) {
    int k = 0;
    for (int j = 0; j < 8; j++) k += (i >> j & 1);
    do {
      int l = s[k], r = 1e3 / 8 + 10;
      if (check(l, i))
        ans = max(ans, l * 8 + k);
      else
        continue;
      while (l < r - 1) {
        int mid = l + r >> 1;
        if (check(mid, i))
          l = mid;
        else
          r = mid;
      }
      ans = max(ans, l * 8 + k);
      s[k] = l;
    } while (next_permutation(b, b + 8));
  }
  cout << ans;
  return 0;
}
