#include <bits/stdc++.h>
using namespace std;
ifstream in("data.in");
ofstream out("data.out");
const int DIM = 1005;
const int EXP = 1 << 8;
const int INF = 0x3f3f3f3f;
int arr[DIM], dp[DIM][EXP], nxt[DIM][DIM][10], f[10];
bool solve(int val, int n) {
  memset(dp, -INF, sizeof(dp));
  dp[1][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int msk = 0; msk < (1 << 8); msk++) {
      dp[i + 1][msk] = std::max(dp[i + 1][msk], dp[i][msk]);
      if (((msk >> arr[i]) & 1) == 0 && nxt[i][val][arr[i]] != n + 1) {
        int p = nxt[i][val][arr[i]];
        dp[p + 1][msk + (1 << arr[i])] =
            std::max(dp[p + 1][msk + (1 << arr[i])], dp[i][msk] + val);
      }
      if (((msk >> arr[i]) & 1) == 0 && nxt[i][val + 1][arr[i]] != n + 1) {
        int p = nxt[i][val + 1][arr[i]];
        dp[p + 1][msk + (1 << arr[i])] =
            std::max(dp[p + 1][msk + (1 << arr[i])], dp[i][msk] + val + 1);
      }
    }
  }
  return ((dp[n + 1][(1 << 8) - 1] >= 0) ? true : false);
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    arr[i]--;
  }
  for (int i = 1; i <= n + 1; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 0; k <= 7; k++) {
        nxt[i][j][k] = n + 1;
      }
    }
  }
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= n - i + 1; j++) {
      for (int k = 0; k <= 7; k++) {
        if (arr[i] != k) {
          nxt[i][j][k] = nxt[i + 1][j][k];
        } else {
          if (j == 1) {
            nxt[i][j][k] = i;
          } else
            nxt[i][j][k] = nxt[i + 1][j - 1][k];
        }
      }
    }
  }
  int l = 1, r = n;
  while (l <= r) {
    int m = l + (r - l) / 2;
    if (solve(m, n) == true) {
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  if (r != 0) {
    solve(r, n);
    cout << dp[n + 1][(1 << 8) - 1];
  } else {
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (f[arr[i]] == 0) {
        f[arr[i]] = 1;
        ans++;
      }
    }
    cout << ans;
  }
  return 0;
}
