Problem: Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!

Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks. 

This time Alice wants to form four melodies for her tracks.

Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.

You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.

Input

The first line contains one integer number n (4 ≤ n ≤ 3000).

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — notes written on a sheet.

Output

Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.

Examples

Input

5
1 3 5 7 9


Output

4


Input

5
1 3 5 7 2


Output

5

Note

In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).

In the second example it is possible to compose one melody with 2 notes — {1, 2}. Remaining notes are used in other three melodies (one note per each melody).

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[200006];
namespace flow {
const int MAXM = 1.2e7 + 6;
int N;
int head[200006], to[MAXM], nex[MAXM], wto[MAXM], cto[MAXM], cur[200006],
    ecn = -1;
inline void Ade(int u, int v, int w, int c) {
  to[++ecn] = v, nex[ecn] = head[u], wto[ecn] = w, cto[ecn] = c, head[u] = ecn;
}
inline void ade(int u, int v, int w, int c) {
  Ade(u, v, w, c), Ade(v, u, 0, -c);
}
void in(int t) {
  N = t;
  for (int i = (1), iend = (N); i <= iend; ++i) head[i] = -1;
  ecn = -1;
}
queue<int> Q;
int dis[200006], vis[200006];
const int inf = 1e9;
int s, t;
bool spfa() {
  memset(dis, -0x3f, sizeof dis), memset(vis, 0, sizeof vis);
  for (int i = (1), iend = (N); i <= iend; ++i) cur[i] = head[i];
  dis[s] = 0, vis[s] = 1;
  Q.push(s);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    vis[u] = 0;
    for (int i = head[u]; ~i; i = nex[i])
      if (wto[i] && dis[to[i]] < dis[u] + cto[i]) {
        int v = to[i];
        dis[v] = dis[u] + cto[i];
        if (!vis[v]) Q.push(v), vis[v] = 1;
      }
  }
  return dis[t] > -inf;
}
int co;
int dfs(int u, int lim) {
  if (!lim || u == t) return lim;
  vis[u] = 1;
  int f, flow = 0;
  for (int& i = cur[u]; ~i; i = nex[i])
    if (!vis[to[i]] && wto[i] && dis[to[i]] == dis[u] + cto[i] &&
        (f = dfs(to[i], min(lim, wto[i])))) {
      lim -= f, flow += f, wto[i] -= f, wto[i ^ 1] += f;
      co += f * cto[i];
      if (!lim) break;
    }
  vis[u] = 0;
  return flow;
}
int dinic() {
  int re = 0;
  co = 0;
  while (spfa()) {
    re += dfs(s, 0x3f3f3f3f);
  }
  return co;
}
}  // namespace flow
int in[200006], ot[200006], oo[200006], o1[200006], cnt;
void solve() {
  cin >> n;
  flow::in(n * 4 + 6);
  for (int i = (1), iend = (n); i <= iend; ++i) {
    scanf("%d", A + i), in[i] = ++cnt, ot[i] = ++cnt, oo[i] = ++cnt,
                        o1[i] = ++cnt;
    flow::ade(in[i], ot[i], 1, 1);
  }
  int s, t, tr;
  s = ++cnt, t = ++cnt;
  tr = ++cnt;
  flow::ade(s, tr, 4, 0);
  flow::s = s, flow::t = t;
  for (int i = (1), iend = (n); i <= iend; ++i) {
    flow::ade(tr, in[i], 1, 0), flow::ade(ot[i], t, 1, 0);
    flow::ade(o1[i], in[i], 1, 0), flow::ade(oo[i], in[i], 1, 0);
    for (int j = (i + 1), jend = (n); j <= jend; ++j)
      if (A[j] % 7 == A[i] % 7) {
        flow::ade(ot[i], oo[j], 1, 0);
        flow::ade(oo[i], oo[j], 4, 0);
        break;
      }
    for (int j = (i + 1), jend = (n); j <= jend; ++j)
      if (A[j] == A[i] - 1) {
        flow::ade(ot[i], o1[j], 1, 0);
        break;
      }
    for (int j = (i + 1), jend = (n); j <= jend; ++j)
      if (A[j] == A[i] + 1) {
        flow::ade(ot[i], o1[j], 1, 0);
        break;
      }
    for (int j = (i + 1), jend = (n); j <= jend; ++j)
      if (A[j] == A[i]) {
        flow::ade(o1[i], o1[j], 4, 0);
        break;
      }
  }
  cout << flow::dinic() << endl;
}
signed main() { solve(); }