#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream &operator>>(istream &__ins_, vector<T> &__v_) {
  for (T &__dst_ : __v_) __ins_ >> __dst_;
  return __ins_;
}
template <typename F, typename S>
istream &operator>>(istream &__ins_, pair<F, S> &__p_) {
  return __ins_ >> __p_.first >> __p_.second;
}
void in() {}
template <typename T, typename... Args>
void in(T &__dst_, Args &...__args_) {
  cin >> __dst_;
  in(__args_...);
}
template <typename F, typename S>
ostream &operator<<(ostream &__outs_, pair<F, S> const &__p_) {
  return __outs_ << "(" << __p_.first << ", " << __p_.second << ")";
}
template <typename T>
ostream &operator<<(ostream &__outs_, vector<T> const &__v_) {
  for (size_t i = 0; i < __v_.size(); ++i) {
    if (i > 0) __outs_ << " ";
    __outs_ << __v_[i];
  }
  return __outs_;
}
void out(char const *, ostream &) {}
template <typename T, typename... Args>
void out(char const *__prefix_, ostream &__outs_, T const &__src_,
         Args const &...__args_) {
  out(__prefix_, __outs_ << __prefix_ << __src_, __args_...);
}
template <typename T, typename... Args>
void out(T const &__src_, Args const &...__args_) {
  out(" ", cout << __src_, __args_...);
}
void outln() { cout << endl; }
template <typename... Args>
void outln(Args const &...__args_) {
  out(__args_...);
  out("\n");
}
int const mod = 1e9 + 7;
int const inf = (int)1e9;
long long const infl = (long long)2e18;
double const eps = 1e-10;
double const seps = 1e-7;
void solve();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout << setprecision(numeric_limits<double>::digits10);
  cerr << setprecision(numeric_limits<long double>::digits10);
  solve();
  return 0;
}
static int const N = 1000;
static int const B = 125;
static int n;
static int const P = 40320;
static int ps[P][8];
static int ms[8][70];
static int mn[8];
static int p2n[N + 1][8];
static int n2p[N + 1][8];
static int jt[B + 1][N + 1][8];
static void genps() {
  for (int i = 0; i < 8; ++i) {
    ps[0][i] = i;
  }
  for (int i = 1; i < P; ++i) {
    copy(begin(ps[i - 1]), end(ps[i - 1]), begin(ps[i]));
    next_permutation(begin(ps[i]), end(ps[i]));
  }
}
static void genms() {
  for (int m = 0; m < 255; ++m) {
    int c = 0;
    int t = m;
    while (t) {
      c += t & 1;
      t >>= 1;
    }
    ms[c][mn[c]++] = m;
  }
}
static void genjt(vector<int> const &a) {
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 8; ++j) {
      p2n[i][j] = p2n[i - 1][j];
      n2p[i][j] = inf;
      jt[0][i][j] = i;
    }
    ++p2n[i][a[i - 1] - 1];
  }
  for (int i = n; i >= 0; --i) {
    for (int j = 0; j < 8; ++j) {
      n2p[p2n[i][j]][j] = i;
    }
  }
  for (int b = 1; b <= B; ++b) {
    for (int i = 0; i <= n; ++i) {
      for (int j = 0; j < 8; ++j) {
        int const c = p2n[i][j] + b;
        jt[b][i][j] = (c <= n) ? n2p[c][j] : inf;
      }
    }
  }
}
static inline bool check(int const b, int const m, int const pi) {
  int i = 0;
  for (int j = 0; j < 8 && i <= n; ++j) {
    int const x = ps[pi][j];
    i = jt[b + ((m >> x) & 1)][i][x];
  }
  return i <= n;
}
static inline bool check_s(int const b, int const s, int const pi) {
  int const c = mn[s];
  for (int k = 0; k < c; ++k) {
    if (check(b, ms[s][k], pi)) {
      return true;
    }
  }
  return false;
}
void solve() {
  genps();
  genms();
  in(n);
  vector<int> a(n);
  in(a);
  genjt(a);
  int const maxb = n / 8;
  int b = 0;
  int pi = 0;
  while (b < maxb) {
    int tpi = pi;
    while (tpi < P && !check(b + 1, 0, tpi)) {
      ++tpi;
    }
    if (tpi < P) {
      ++b;
      pi = tpi;
    } else {
      break;
    }
  }
  int const maxs = min(n - 8 * b, 7);
  int s = 0;
  while (s < maxs) {
    int tpi = pi;
    while (tpi < P && !check_s(b, s + 1, tpi)) {
      ++tpi;
    }
    if (tpi < P) {
      ++s;
      pi = tpi;
    } else {
      break;
    }
  }
  outln(8 * b + s);
}
