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;
struct edge {
  int to, cap, cost, rev;
};
const int maxn = 100010;
const int INF = 1 << 30;
vector<edge> g[maxn];
int s, t;
int maxflow, mincost;
int in[maxn], out[maxn], num[maxn];
int c[maxn], h[maxn];
int d[maxn], a[maxn];
pair<int, int> p[maxn];
queue<int> que;
bool inq[maxn];
void link(int u, int v, int cap, int cost) {
  if (u == 0 || v == 0) return;
  g[u].push_back((edge){v, cap, cost, (int)g[v].size()});
  g[v].push_back((edge){u, 0, -cost, (int)g[u].size() - 1});
}
void findpath() {
  for (int i = 1; i <= t; i++) d[i] = a[i] = INF, p[i] = make_pair(0, 0);
  memset(inq, false, sizeof(inq));
  d[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    inq[u] = false;
    for (int i = 0; i < (int)g[u].size(); i++) {
      edge e = g[u][i];
      int v = e.to;
      if (e.cap > 0 && d[v] > d[u] + e.cost) {
        d[v] = d[u] + e.cost;
        a[v] = min(a[v], e.cap);
        p[v] = make_pair(u, i);
        if (!inq[v]) {
          inq[v] = true;
          que.push(v);
        }
      }
    }
  }
  maxflow += a[t];
  mincost += a[t] * d[t];
  int t2 = t;
  while (p[t2].first) {
    g[p[t2].first][p[t2].second].cap -= a[t];
    g[t2][g[p[t2].first][p[t2].second].rev].cap += a[t];
    t2 = p[t2].first;
  }
}
int mcmf() {
  for (int i = 1; i <= 4; i++) findpath();
  return mincost;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
  int cnt = 0;
  for (int i = 1; i <= n; i++) in[i] = ++cnt, out[i] = ++cnt;
  for (int i = n; i >= 1; i--) {
    int last = c[num[i]];
    c[num[i]] = ++cnt;
    link(c[num[i]], last, 1, 0);
    link(out[i], c[num[i] + 1], 1, 0);
    link(out[i], c[num[i] - 1], 1, 0);
    link(c[num[i]], in[i], 1, 0);
  }
  for (int i = n; i >= 1; i--) {
    int last = h[num[i] % 7];
    h[num[i] % 7] = ++cnt;
    link(h[num[i] % 7], last, 1, 0);
    link(out[i], last, 1, 0);
    link(h[num[i] % 7], in[i], 1, 0);
  }
  s = ++cnt, t = ++cnt;
  for (int i = 1; i <= n; i++) {
    link(in[i], out[i], 1, -1);
    link(s, in[i], 1, 0);
    link(out[i], t, 1, 0);
  }
  printf("%d\n", -mcmf());
  return 0;
}