#include <bits/stdc++.h>
using namespace std;
int n, a[222222], dp[1 << 10], dp2[1 << 10][10], cnt[8];
int get(int i, int k, int val) {
  for (int j = i; j <= n; j++) {
    if (a[j] == val) {
      k--;
    }
    if (!k) {
      return j + 1;
    }
  }
  return 2000000000;
}
bool ok(int len) {
  dp[0] = 1;
  for (int mask = 1; mask < (1 << 8); mask++) {
    dp[mask] = 2000000000;
  }
  for (int mask = 1; mask < (1 << 8); mask++) {
    for (int i = 0; i < 8; i++) {
      if ((1 << i) & mask) {
        dp[mask] = min(dp[mask], get(dp[mask ^ (1 << i)], len, i + 1));
      }
    }
  }
  return dp[(1 << 8) - 1] != 2000000000;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  int l = 0, r = n + 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (ok(mid)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  if (!ok(r)) {
    r--;
  }
  if (!r) {
    int res = 0;
    for (int i = 1; i <= 8; i++) {
      if (cnt[i]) {
        res++;
      }
    }
    cout << res << endl;
    return 0;
  }
  for (int mask = 0; mask < (1 << 8); mask++) {
    for (int i = 0; i <= 8; i++) {
      dp2[mask][i] = 2000000000;
    }
  }
  dp2[0][0] = 1;
  for (int mask = 1; mask < (1 << 8); mask++) {
    for (int i = 0; i < 8; i++) {
      if ((1 << i) & mask) {
        for (int j = 1; j <= 8; j++) {
          dp2[mask][j] =
              min(dp2[mask][j], get(dp2[mask ^ (1 << i)][j - 1], r + 1, i + 1));
          dp2[mask][j] =
              min(dp2[mask][j], get(dp2[mask ^ (1 << i)][j], r, i + 1));
        }
      }
      dp2[mask][0] = min(dp2[mask][0], get(dp2[mask ^ (1 << i)][0], r, i + 1));
    }
  }
  int bar = 0;
  for (int i = 1; i <= 8; i++) {
    if (dp2[(1 << 8) - 1][i] != 2000000000) {
      bar = i;
    }
  }
  cout << 8 * r + bar << endl;
  return 0;
}
