#include <bits/stdc++.h>
using namespace std;
int n;
int can(vector<vector<int> > &in, vector<int> &a, int len) {
  vector<int> cur(8);
  int i, j;
  for (i = 0; i < 8; i++) cur[i] = 0;
  vector<vector<int> > dp(n + 1, vector<int>(1 << 8, INT_MIN));
  for (i = 0; i <= n; i++) {
    for (j = 0; j < 1 << 8; j++) dp[i][j] = INT_MIN;
  }
  dp[0][0] = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < 1 << 8; j++) {
      if (dp[i][j] == INT_MIN) continue;
      int k;
      for (k = 0; k < 8; k++) {
        if (j & (1 << k)) continue;
        int it = cur[k] + len - 1;
        if (it < in[k + 1].size()) {
          dp[in[k + 1][it] + 1][j | (1 << k)] =
              max(dp[in[k + 1][it] + 1][j | (1 << k)], dp[i][j]);
        }
        it++;
        if (it < in[k + 1].size()) {
          dp[in[k + 1][it] + 1][j | (1 << k)] =
              max(dp[in[k + 1][it] + 1][j | (1 << k)], dp[i][j] + 1);
        }
      }
    }
    cur[a[i] - 1]++;
  }
  int ans = INT_MIN;
  for (i = 0; i <= n; i++) ans = max(ans, dp[i][(1 << 8) - 1]);
  if (ans == INT_MIN)
    return -1;
  else
    return ans * (len + 1) + (8 - ans) * len;
}
int main() {
  ios::sync_with_stdio(true);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  vector<int> a(n);
  int i, j;
  vector<vector<int> > in(9);
  for (i = 0; i < n; i++) {
    cin >> a[i];
    in[a[i]].push_back(i);
  }
  int l = 1, r = n / 8;
  int ans = INT_MIN;
  while (r - l > 1 && r >= l) {
    int mid = (l + r) / 2;
    if (can(in, a, mid) == -1) {
      r = mid;
    } else {
      l = mid;
    }
  }
  ans = max(can(in, a, l), can(in, a, r));
  if (ans == -1) {
    ans = 0;
    for (i = 1; i <= 8; i++) {
      if (in[i].size() != 0) ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
