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;
const double PI = acos(-1);
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
long long add(long long a, long long b) {
  long long ret = a + b;
  if (ret >= 1000000007) ret -= 1000000007;
  return ret;
}
long long subtract(long long a, long long b) {
  long long ret = a - b;
  if (ret < 0) ret += 1000000007;
  return ret;
}
long long mult(long long a, long long b) {
  long long ret = a * b;
  if (ret >= 1000000007) ret %= 1000000007;
  return ret;
}
long long bigmod(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = mult(ret, a);
    b >>= 1;
    a = mult(a, a);
  }
  return ret;
}
long long inverse(long long n) { return bigmod(n, 1000000007 - 2); }
bool base[1000010];
vector<int> primes;
void sieve(int mx) {
  mx += 10;
  int x = sqrt(mx);
  for (int i = 3; i <= x; i += 2)
    if (base[i] == 0)
      for (int j = i * i, k = i << 1; j < mx; j += k) base[j] = 1;
  primes.push_back(2);
  for (int i = 3; i < mx; i += 2)
    if (base[i] == 0) primes.push_back(i);
}
bool checkBit(int n, int i) { return (n & (1 << i)); }
int setBit(int n, int i) { return (n | (1 << i)); }
int resetBit(int n, int i) { return (n & (~(1 << i))); }
vector<int> v;
int n, cases, vis[8][1002][1002][2], dp[8][1002][1002][2], a[1002],
    sum[8][1002];
int f(int i, int j, int rem, int flag) {
  if (i == 8) return 0;
  int &ret = dp[i][j][rem][flag];
  if (vis[i][j][rem][flag] == cases) return ret;
  vis[i][j][rem][flag] = cases;
  ret = -100000000;
  int tmp = lower_bound(sum[v[i]] + j, sum[v[i]] + n + 1, rem + sum[v[i]][j]) -
            sum[v[i]] - 1;
  if (tmp < n) {
    ret = rem + f(i + 1, tmp + 1, rem, flag);
    ret = max(ret, rem + f(i + 1, tmp + 1, rem + (flag ? -1 : 1), flag ^ 1));
  }
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) {
    --a[i];
    for (int j = 0; j < 8; ++j) sum[j][i] = sum[j][i - 1];
    ++sum[a[i]][i];
  }
  for (int i = 0; i < 8; ++i) v.push_back(i);
  int res = 0;
  do {
    int l = 0, h = n, ans = -1;
    ++cases;
    while (l <= h) {
      int m = (l + h) / 2;
      int p = f(0, 0, m, 0);
      int q = f(0, 0, m + 1, 1);
      ans = max(ans, max(p, q));
      if (max(p, q) > 0)
        l = m + 1;
      else
        h = m - 1;
    }
    res = max(res, ans);
  } while (next_permutation(v.begin(), v.end()));
  printf("%d\n", res);
  return 0;
}
