#include <bits/stdc++.h>   using namespace std;   const int maxn = 1050;   int dp[maxn][9], nxt[maxn][9][maxn], a[maxn], n, cnt[9];   inline int ask(int k) { return (1 << (k - 1)); }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= 8; ++j) cnt[j] = 0;       for (int j = 1; j <= 8; ++j)         for (int k = 1; k <= n; ++k) nxt[i][j][k] = 1e9;       for (int j = i; j <= n; ++j) nxt[i][a[j]][++cnt[a[j]]] = j;     }     int ans = 0;     for (int num = 0; num * 8 <= n; ++num) {       for (int i = 0; i < 256; ++i)         for (int k = 0; k <= 8; ++k) dp[i][k] = 1e9;       dp[0][0] = 1;       for (int i = 0; i < 256; ++i)         for (int j = 0; j <= 8; ++j)           for (int k = 1; k <= 8; ++k) {             if (dp[i][j] > n || ask(k) & i) continue;             dp[i ^ ask(k)][j] = min(dp[i ^ ask(k)][j], nxt[dp[i][j]][k][num] + 1);             dp[i ^ ask(k)][j + 1] =                 min(dp[i ^ ask(k)][j + 1], nxt[dp[i][j]][k][num + 1] + 1);           }       for (int i = 0; i <= 8; ++i)         if (dp[255][i] <= n + 1) ans = max(ans, 8 * num + i);     }     cout << ans << endl;     return 0;   }   