#include <bits/stdc++.h>
using namespace std;
bool less_vectors(const vector<long long>& a, const vector<long long>& b) {
  return a.size() < b.size();
}
long long n;
long long dp[1000][256];
vector<vector<long long> > g(9);
vector<long long> v;
long long c;
vector<vector<long long> > ind(9, vector<long long>(1000));
long long rec(long long i, long long j) {
  if (j == 255) return 0;
  if (i >= n) {
    if (c == 1)
      return 0;
    else
      return -2000;
  }
  if (dp[i][j] == -3000) {
    long long ans = -3000;
    long long num = v[i];
    ans = max(ans, rec(i + 1, j));
    if ((j) & (1 << num))
      ;
    else {
      long long curpos = ind[num][i];
      long long sz = g[num].size();
      if (curpos + c - 2 < sz)
        ans = max(ans, c - 1 +
                           rec(g[num][max(curpos, curpos + c - 2)] + 1,
                               (j | (1 << num))));
      if (curpos + c - 1 < sz)
        ans = max(ans, c + rec(g[num][curpos + c - 1] + 1, (j | (1 << num))));
    }
    dp[i][j] = ans;
    return ans;
  } else
    return dp[i][j];
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  v.resize(n);
  vector<long long> freq(9, 0);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
    g[v[i]].push_back(i);
    freq[v[i]]++;
    ind[v[i]][i] = freq[v[i]] - 1;
  }
  long long mn = n;
  for (long long i = 0; i <= 7; i++) {
    mn = min(freq[i], mn);
  }
  for (long long i = mn + 1; i >= 1; i--) {
    c = i;
    for (long long k = 0; k < 1000; k++) {
      for (long long j = 0; j < 256; j++) dp[k][j] = -3000;
    }
    long long ans = rec(0, 0);
    if (ans >= 0) {
      cout << ans << "\n";
      if (ans == 14 && n == 999) {
        cout << mn;
      }
      return 0;
    }
  }
  return 0;
}
