#include <bits/stdc++.h>
using namespace std;
int a[10004];
int nextDp[1004][9][150];
int dp[1004][(1 << 8) + 2][126];
int n;
int rec(int i, int mask, int mn) {
  if (i >= n) {
    if (mask == 255) return 0;
    return -1e8;
  }
  int &ret = dp[i][mask][mn];
  if (ret != -1) return ret;
  ret = -1e9;
  ret = max(ret, rec(i + 1, mask, mn));
  if (!(mask & (1 << (a[i] - 1)))) {
    int to = nextDp[i][a[i]][mn - 1];
    if (to != 1e9)
      ret = max(ret, mn + rec(to + 1, mask | (1 << (a[i] - 1)), mn));
    int to1 = nextDp[i][a[i]][mn];
    if (to1 != 1e9)
      ret = max(ret, (mn + 1) + rec(to1 + 1, mask | (1 << (a[i] - 1)), mn));
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  set<int> tt;
  cin >> n;
  memset(dp, -1, sizeof dp);
  int i, j, k;
  for (i = 0; i <= 1002; i++) {
    for (j = 0; j < 9; j++) {
      for (k = 0; k < 150; k++) nextDp[i][j][k] = 1e9;
    }
  }
  for (i = 0; i < n; i++) cin >> a[i], tt.insert(a[i]);
  for (i = n - 1; i >= 0; i--) {
    for (j = 0; j < 9; j++) {
      for (k = 0; k < 150; k++) {
        if (j == a[i] && k)
          nextDp[i][j][k] = nextDp[i + 1][j][k - 1];
        else
          nextDp[i][j][k] = nextDp[i + 1][j][k];
      }
    }
    nextDp[i][a[i]][0] = i;
  }
  int mx = 0;
  for (i = 1; i <= 125; i++) mx = max(mx, rec(0, 0, i));
  int mx1 = tt.size();
  cout << max(mx, mx1);
  return 0;
}
