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((double)-1);
const long long oo((long long)1e+18 + 100);
template <typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
  for (auto &it : v) out << it << ' ';
  return out;
}
template <typename T>
inline istream &operator>>(istream &in, vector<T> &v) {
  for (auto &it : v) in >> it;
  return in;
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << p.first << ' ' << p.second << ' ';
  return out;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}
template <typename T>
inline ostream &operator<<(ostream &out, const set<T> &p) {
  for (auto &i : p) out << i << ' ';
  return out;
}
template <typename T>
inline istream &operator>(istream &in, T &p) {
  return in >> p;
}
template <typename T>
inline ostream &operator<(ostream &out, const T &p) {
  return out << p;
}
template <typename T1, typename T2>
inline pair<T1 &, T2 &> unzip_pair(T1 &p1, T2 &p2) {
  return pair<T1 &, T2 &>(p1, p2);
}
template <typename T>
char $(T a) {
  cerr < a;
  return ' ';
}
template <typename T1, typename... T2>
char $(T1 a1, T2... a2) {
  $(a1);
  cerr < ", ";
  $(a2...);
  return ' ';
}
int pref[1000][8];
int dp[1001][1 << 8];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout < fixed < setprecision(7);
  int n;
  cin > n;
  vector<int> mas(n);
  cin > mas;
  vector<int> chi[8];
  pref[0][mas[0] - 1] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 8; j++) {
      pref[i][j] = pref[i - 1][j];
    }
    pref[i][mas[i] - 1]++;
  }
  for (int i = 0; i < n; i++) {
    chi[mas[i] - 1].push_back(i);
  }
  int l = 0;
  int r = 1e+9;
  for (int i = 0; i < 8; i++) {
    r = min(r, (int)chi[i].size());
  }
  r = max(r, 1);
  int ans;
  while (r > l) {
    int m = (l + r + 1) / 2;
    memset(dp, 255, sizeof dp);
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << 8); j++) {
        if (dp[i][j] == -1) continue;
        for (int k = 0; k < 8; k++) {
          if ((1 << k) & j) continue;
          int ms = j | (1 << k);
          int ps2 = m;
          if (i) ps2 += pref[i - 1][k];
          if (chi[k].size() >= ps2) {
            if (ps2 == 0) {
              dp[ps2][ms] = max(dp[ps2][ms], dp[i][j]);
            } else {
              dp[chi[k][ps2 - 1] + 1][ms] =
                  max(dp[chi[k][ps2 - 1] + 1][ms], dp[i][j]);
            }
          }
          ps2++;
          if (chi[k].size() >= ps2) {
            dp[chi[k][ps2 - 1] + 1][ms] =
                max(dp[chi[k][ps2 - 1] + 1][ms], dp[i][j] + 1);
          }
        }
      }
    }
    ans = -1;
    for (int i = 0; i <= n; i++) {
      ans = max(ans, dp[i][(1 << 8) - 1]);
    }
    if (ans >= 0) {
      l = m;
    } else {
      r = m - 1;
      if (r == l) l--;
    }
  }
  cout < (8 - ans) * l + (l + 1) * ans;
  return 0;
}
