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;
inline int read() {
  int x;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    ;
  for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';)
    x = (x << 3) + (x << 1) + c - '0';
  return x;
}
struct edge {
  int nx, t, w, c;
} e[50000 * 2 + 5];
int a[3000 + 5], h[6000 + 5], en = 1, d[6000 + 5], q[6000 + 5], qn,
                              inq[6000 + 5], r[6000 + 5];
vector<int> v[100000 + 5], vv[7];
inline void ins(int x, int y, int w, int c) {
  e[++en] = (edge){h[x], y, w, c};
  h[x] = en;
  e[++en] = (edge){h[y], x, 0, -c};
  h[y] = en;
}
inline int next(int x) { return x < 6000 + 4 ? x + 1 : 0; }
bool spfa() {
  int i, j;
  memset(d, 200, sizeof(d));
  for (d[q[qn = 1, i = 0] = 6000 + 1] = 0; i != qn; inq[q[i]] = 0, i = next(i))
    for (j = h[q[i]]; j; j = e[j].nx)
      if (e[j].w && d[q[i]] + e[j].c > d[e[j].t]) {
        d[e[j].t] = d[q[i]] + e[j].c;
        r[e[j].t] = j;
        if (!inq[e[j].t]) inq[q[qn] = e[j].t] = 1, qn = next(qn);
      }
  return d[6000 + 2] > d[0];
}
int main() {
  int n = read(), i, j, ans = 0, mn;
  for (i = 1; i <= n; ++i)
    v[a[i] = read()].push_back(i), vv[a[i] % 7].push_back(i);
  ins(6000 + 1, 6000 + 2 + 1, 4, 0);
  for (i = 1; i <= n; ++i) {
    ins(6000 + 2 + 1, i, 1, 0);
    ins(i, i + n, 1, 1);
    ins(i + n, 6000 + 2, 1, 0);
    vector<int>::iterator it;
    it = upper_bound(v[a[i] - 1].begin(), v[a[i] - 1].end(), i);
    for (j = 0; j < 4; ++j)
      if (it != v[a[i] - 1].end())
        ins(i + n, *it++, 1, 0);
      else
        break;
    it = upper_bound(v[a[i] + 1].begin(), v[a[i] + 1].end(), i);
    for (j = 0; j < 4; ++j)
      if (it != v[a[i] + 1].end())
        ins(i + n, *it++, 1, 0);
      else
        break;
    it = upper_bound(vv[a[i] % 7].begin(), vv[a[i] % 7].end(), i);
    for (j = 0; j < 4; ++j)
      if (it != vv[a[i] % 7].end())
        ins(i + n, *it++, 1, 0);
      else
        break;
  }
  while (spfa()) {
    for (mn = 0x7FFFFFFF, i = 6000 + 2; i != 6000 + 1; i = e[r[i] ^ 1].t)
      mn = min(mn, e[r[i]].w);
    for (i = 6000 + 2; i != 6000 + 1; i = e[r[i] ^ 1].t)
      e[r[i]].w -= mn, e[r[i] ^ 1].w += mn;
    ans += mn * d[6000 + 2];
  }
  printf("%d", ans);
}