Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions:

  * the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are ck cards with number k on them in the subsequence, than for all pairs of integers <image> the condition |ci - cj| ≤ 1 must hold. 
  * if there is at least one card with number x on it in the subsequence, then all cards with number x in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1, 1, 2, 2] satisfies this condition while the subsequence [1, 2, 2, 1] doesn't. Note that [1, 1, 2, 2] doesn't satisfy the first condition. 



Please help Vladik to find the length of the longest subsequence that satisfies both conditions.

Input

The first line contains single integer n (1 ≤ n ≤ 1000) — the number of cards in Vladik's sequence.

The second line contains the sequence of n positive integers not exceeding 8 — the description of Vladik's sequence.

Output

Print single integer — the length of the longest subsequence of Vladik's sequence that satisfies both conditions.

Examples

Input

3
1 1 1


Output

1

Input

8
8 7 6 5 4 3 2 1


Output

8

Input

24
1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8


Output

17

Note

In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.
#include <bits/stdc++.h>
using namespace std;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
template <typename T>
T mulmod(T a, T b, T m) {
  long long int q =
      (long long int)(((long double)a * (long double)b) / (long double)m);
  long long int r = a * b - q * m;
  if (r > m) r %= m;
  if (r < 0) r += m;
  return r;
}
template <typename T>
T MOD(T a, T b) {
  return (a < b ? a : a % b);
}
template <typename T>
T expo(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T>
T power(T e, T n, T m) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = mulmod(x, p, m);
    p = mulmod(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T>
T gcd(T a, T b) {
  if (a % b) return gcd(b, a % b);
  return b;
}
template <typename T>
T lcm(T a, T b) {
  return (a * (b / gcd(a, b)));
}
template <typename T>
T add(T a, T b, T Mod) {
  a += b;
  return (a >= Mod ? a - Mod : a);
}
template <typename T>
T sub(T a, T b, T Mod) {
  a -= b;
  return (a < 0 ? a + Mod : a);
}
int cnt[9][1005], a[1005], ans = 1;
int DP[1005][1 << 8], n;
int MX = (1 << 8) - 1, lim;
int solve(int idx, int mask) {
  if (idx == n + 1) return mask == MX ? 0 : -INF;
  int &res = DP[idx][mask];
  if (res != -1) return DP[idx][mask];
  res = max(res, solve(idx + 1, mask));
  if ((mask & (1 << a[idx] - 1)) == 0) {
    int nidx = lower_bound(cnt[a[idx]] + 1, cnt[a[idx]] + n + 1,
                           cnt[a[idx]][idx] + lim - 1) -
               cnt[a[idx]];
    if (nidx <= n) {
      res = max(res, lim + solve(nidx + 1, mask | 1 << a[idx] - 1));
    }
    nidx = lower_bound(cnt[a[idx]] + 1, cnt[a[idx]] + n + 1,
                       cnt[a[idx]][idx] + lim) -
           cnt[a[idx]];
    if (nidx <= n) {
      res = max(res, lim + 1 + solve(nidx + 1, mask | 1 << a[idx] - 1));
    }
  }
  if (res < 0) res = -INF;
  return res;
}
bool check(int mid, int n) {
  lim = mid;
  memset(DP, -1, sizeof(DP));
  int res = solve(1, 0);
  if (res <= 0) return false;
  ans = max(ans, res);
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  set<int> s;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    s.insert(a[i]);
    for (int j = 1; j <= 8; j++) {
      cnt[j][i] = cnt[j][i - 1];
    }
    ++cnt[a[i]][i];
  }
  int lo = 1, hi = n / 8;
  while (lo <= hi) {
    int mid = lo + hi >> 1;
    if (check(mid, n)) {
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  ans = max(ans, (int)s.size());
  cout << ans << "\n";
  return 0;
}
