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;
long long exp(long long b, long long e) {
  if (b == 1 || e == 0) return 1;
  if (e == 1) return b % 1000000007;
  if (e == 2) return (b * b) % 1000000007;
  return ((e % 2 == 0)
              ? exp(exp(b, e / 2), 2) % 1000000007
              : (b * (exp(exp(b, e / 2), 2) % 1000000007)) % 1000000007);
}
long long gcd(long long a, long long b) {
  return ((b == 0) ? a : gcd(b, a % b));
}
int pw[10], a[1111], dp[1111][257], n;
vector<int> idx[9];
int possible(int pos, int mask, int cnt) {
  if (dp[pos][mask] != -1) return dp[pos][mask];
  if (pos >= n || mask == 255) {
    if (mask == 255 || cnt == 0) return (dp[pos][mask] = 0);
    return (dp[pos][mask] = -1);
  }
  int ret = -1;
  for (int i = 0; i < 8; ++i) {
    if (mask & pw[i]) continue;
    int s = -1, e = idx[i + 1].size(), mid, t;
    if (e == 0 && cnt > 0) return (dp[pos][mask] = -1);
    while (e - s > 1) {
      mid = (s + e) / 2;
      if (idx[i + 1][mid] < pos)
        s = mid;
      else
        e = mid;
    }
    if (e + cnt - 1 >= (int)idx[i + 1].size()) return (dp[pos][mask] = -1);
    if (e + cnt > 0) {
      t = possible(idx[i + 1][e + cnt - 1] + 1, mask | pw[i], cnt);
      if (t != -1) {
        ret = (((ret) > (cnt + t)) ? (ret) : (cnt + t));
      }
    } else {
      t = possible(pos, mask | pw[i], cnt);
      if (t != -1) {
        ret = (((ret) > (cnt + t)) ? (ret) : (cnt + t));
      }
    }
    if (e + cnt < idx[i + 1].size()) {
      t = possible(idx[i + 1][e + cnt] + 1, mask | pw[i], cnt);
      if (t != -1) {
        ret = (((ret) > (cnt + 1 + t)) ? (ret) : (cnt + 1 + t));
      }
    }
  }
  return (dp[pos][mask] = ret);
}
int check[10] = {0};
int main() {
  int i, j, k;
  pw[0] = 1;
  for (i = 1; i < 10; ++i) pw[i] = 2 * pw[i - 1];
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    idx[a[i]].push_back(i);
  }
  for (i = 0; i < n; ++i) {
    check[a[i]]++;
  }
  int ans = 0;
  int e = (((n) % (8) == 0) ? ((n) / (8)) : ((n) / (8) + 1)), s = -1, mid;
  while (e - s > 1) {
    for (i = 0; i <= n; ++i) {
      for (j = 0; j < 257; ++j) dp[i][j] = -1;
    }
    mid = (e + s) / 2;
    int t = possible(0, 0, mid);
    if (t > 0)
      s = mid;
    else
      e = mid;
    ans = (((ans) > (t)) ? (ans) : (t));
  }
  printf("%d\n", ans);
  return 0;
}
