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).
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int maxn = 20005;
namespace flow {
struct edge {
  int v, c, w, nex;
  edge(int v = 0, int c = 0, int w = 0, int nex = 0)
      : v(v), c(c), w(w), nex(nex) {}
} e[100000];
int head[maxn], a[maxn], dist[maxn], from[maxn], en, s, t;
void init() { memset(head, -1, sizeof head); }
void add(int u, int v, int c, int w) {
  e[en] = edge(v, c, w, head[u]), head[u] = en++;
  e[en] = edge(u, 0, -w, head[v]), head[v] = en++;
}
bool spfa() {
  static bool inq[maxn];
  static int q[maxn], ql, qr;
  for (int i = (s); i <= (t); ++i) dist[i] = -inf;
  ql = qr = 0, q[qr++] = s;
  dist[s] = 0, a[s] = inf;
  while (ql != qr) {
    int u = q[ql++];
    if (ql == maxn) ql = 0;
    inq[u] = 0;
    for (int i = head[u]; ~i; i = e[i].nex)
      if (e[i].c > 0 && dist[u] + e[i].w > dist[e[i].v]) {
        dist[e[i].v] = dist[u] + e[i].w;
        a[e[i].v] = min(a[u], e[i].c);
        from[e[i].v] = i;
        if (!inq[e[i].v]) {
          inq[e[i].v] = 1;
          q[qr++] = e[i].v;
          if (qr == maxn) qr = 0;
        }
      }
  }
  return dist[t] != -inf;
}
int mcmf() {
  int res = 0;
  while (spfa()) {
    res += a[t] * dist[t];
    for (int i = t; i != s; i = e[from[i] ^ 1].v) {
      e[from[i]].c -= a[t];
      e[from[i] ^ 1].c += a[t];
    }
  }
  return res;
}
}  // namespace flow
int n, idcnt, a[maxn], b[7], c[100005];
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) scanf("%d", a + i);
  flow::init();
  flow::s = 0, flow::t = 20000;
  flow::add(0, 1, 4, 0);
  idcnt = 2 * n + 2;
  for (int i = (0); i <= (6); ++i) b[i] = 20000;
  for (int i = (0); i <= (100001); ++i) c[i] = 20000;
  for (int i = (1); i <= (n); ++i) {
    flow::add(i << 1, i << 1 | 1, 1, 1);
    flow::add(1, i << 1, 1, 0);
    int to = b[a[i] % 7];
    flow::add(i << 1 | 1, to, inf, 0);
    flow::add(idcnt, i << 1, inf, 0);
    flow::add(idcnt, to, inf, 0);
    b[a[i] % 7] = idcnt++;
    to = c[a[i]];
    flow::add(idcnt, to, inf, 0);
    flow::add(idcnt, i << 1, inf, 0);
    c[a[i]] = idcnt++;
    flow::add(i << 1 | 1, c[a[i] - 1], inf, 0);
    flow::add(i << 1 | 1, c[a[i] + 1], inf, 0);
  }
  printf("%d\n", flow::mcmf());
  return 0;
}
