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 MOD = 1e9 + 7;
int INF = 1e9;
struct edge {
  int from, to, cap, weight;
  edge *rev;
  edge(int u, int v, int c, int w)
      : from(u), to(v), cap(c), weight(w), rev(NULL) {}
};
int n, s, t;
edge *p[12005];
int dist[12005], vis[12005];
vector<edge *> g[12005];
void addedge(int u, int v, int c, int w) {
  edge *in = new edge(u, v, c, w);
  edge *out = new edge(v, u, 0, -w);
  in->rev = out;
  out->rev = in;
  g[u].push_back(in);
  g[v].push_back(out);
}
int mincostflow() {
  int mxf = 0, mnc = 0;
  for (;;) {
    for (int i = 0; i < n; i++) p[i] = NULL, vis[i] = 0;
    for (int i = 0; i < n; i++) dist[i] = INF;
    queue<int> q;
    q.push(s);
    dist[s] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      vis[u] = 0;
      for (edge *e : g[u]) {
        int v = e->to;
        if (e->cap > 0 && dist[u] + e->weight < dist[v]) {
          dist[v] = dist[u] + e->weight;
          p[v] = e;
          if (!vis[v]) q.push(v), vis[v] = 1;
        }
      }
    }
    if (dist[t] == INF) break;
    int fo = INF;
    for (int u = t; p[u] != NULL; u = p[u]->from) fo = min(fo, p[u]->cap);
    for (int u = t; p[u] != NULL; u = p[u]->from) {
      p[u]->cap -= fo, p[u]->rev->cap += fo;
      mnc += fo * p[u]->weight;
    }
    mxf += fo;
  }
  return mnc;
}
int N, a[3005];
vector<int> idx[100005], idx2[7];
int solve() {
  cin >> N;
  for (int i = 0; i < N; i++) cin >> a[i];
  for (int i = 0; i < N; i++)
    idx[a[i]].push_back(i), idx2[a[i] % 7].push_back(i);
  n = N * 4 + 3;
  s = N * 4, t = N * 4 + 2;
  int midman = N * 4 + 1;
  for (int i = 0; i < N; i++) addedge(i, N + i, 1, -1);
  addedge(s, midman, 4, 0);
  for (int i = 0; i < N; i++) addedge(midman, i, 1, 0);
  for (int i = 0; i < N; i++) addedge(N + i, t, 1, 0);
  for (int i = 1; i <= 100000; i++)
    for (int j = 0; j + 1 < idx[i].size(); j++)
      addedge(N * 2 + idx[i][j], N * 2 + idx[i][j + 1], INF, 0);
  for (int i = 0; i < N; i++) addedge(N * 2 + i, i, 1, 0);
  for (int i = 0; i < 7; i++)
    for (int j = 0; j + 1 < idx2[i].size(); j++)
      addedge(N * 3 + idx2[i][j], N * 3 + idx2[i][j + 1], INF, 0);
  for (int i = 0; i < N; i++) addedge(N * 3 + i, i, 1, 0);
  for (int i = 0; i < N; i++) {
    int nout = N + i;
    auto in1 = upper_bound(idx[a[i] + 1].begin(), idx[a[i] + 1].end(), i);
    if (in1 != idx[a[i] + 1].end()) addedge(nout, N * 2 + *in1, 1, 0);
    auto in2 = upper_bound(idx[a[i] - 1].begin(), idx[a[i] - 1].end(), i);
    if (in2 != idx[a[i] - 1].end()) addedge(nout, N * 2 + *in2, 1, 0);
    auto in3 = upper_bound(idx2[a[i] % 7].begin(), idx2[a[i] % 7].end(), i);
    if (in3 != idx2[a[i] % 7].end()) addedge(nout, N * 3 + *in3, 1, 0);
  }
  cout << -mincostflow() << endl;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  solve();
  return 0;
}