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;
namespace Dango {
const int MAXN = 3005, INF = 0x3f3f3f3f;
struct Edge {
  int from, to, cap, flow, cost;
};
vector<Edge> edges;
vector<int> g[MAXN * 2];
int n, num[MAXN], ans, s, t;
int d[MAXN * 2], a[MAXN * 2], p[MAXN * 2];
bool inq[MAXN * 2];
int abs_(int a) { return a < 0 ? -a : a; }
void add(int u, int v, int cap, int cost) {
  edges.push_back((Edge){u, v, cap, 0, cost});
  edges.push_back((Edge){v, u, 0, 0, -cost});
  int s = edges.size();
  g[u].push_back(s - 2);
  g[v].push_back(s - 1);
}
bool BellmanFord() {
  memset(inq, 0, sizeof(inq));
  memset(a, INF, sizeof(a));
  memset(d, INF, sizeof(d));
  queue<int> q;
  q.push(s);
  d[s] = 0;
  inq[s] = true;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    inq[x] = false;
    for (int i = 0; i < g[x].size(); i++) {
      Edge& e = edges[g[x][i]];
      if (e.cap > e.flow && d[e.to] > d[x] + e.cost) {
        d[e.to] = d[x] + e.cost;
        a[e.to] = min(a[x], e.cap - e.flow);
        p[e.to] = g[x][i];
        if (!inq[e.to]) {
          q.push(e.to);
          inq[e.to] = true;
        }
      }
    }
  }
  if (d[t] == INF) return false;
  ans += a[t] * d[t];
  int x = t;
  while (x != s) {
    edges[p[x]].flow += a[t];
    edges[p[x] ^ 1].flow -= a[t];
    x = edges[p[x]].from;
  }
  return true;
}
int work() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> num[i];
  s = 2 * n + 3;
  t = 2 * n + 5;
  add(s, 0, 4, 0);
  for (int i = 1; i <= n; i++) {
    add(0, 2 * i - 1, INF, 0);
    add(2 * i - 1, 2 * i, 1, -1);
    add(2 * i, t, INF, 0);
    add(2 * i - 1, 2 * i, INF, 0);
  }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (abs_(num[i] - num[j]) % 7 == 0) {
        add(2 * i, 2 * j - 1, INF, 0);
        break;
      }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (num[i] - num[j] == 1) {
        add(2 * i, 2 * j - 1, INF, 0);
        break;
      }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (num[i] - num[j] == -1) {
        add(2 * i, 2 * j - 1, INF, 0);
        break;
      }
  while (BellmanFord())
    ;
  cout << -ans;
  return 0;
}
}  // namespace Dango
int main() { return Dango::work(); }