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 min_cost_max_flow {
  int n, st, en, tot, flowTot;
  int b[9100000], c[9100000], w[9100000], nxt[9100000];
  int head[6015], pre[6015], p[6015], d[6015];
  queue<int> q;
  void init(int _n, int _st, int _en) {
    memset(head, 0, sizeof(head));
    tot = 1;
    st = _st;
    en = _en;
    n = _n;
    flowTot = 0;
  }
  void add(int x, int y, int z, int cost) {
    b[++tot] = y;
    c[tot] = z;
    w[tot] = cost;
    nxt[tot] = head[x];
    head[x] = tot;
    b[++tot] = x;
    c[tot] = 0;
    w[tot] = -cost;
    nxt[tot] = head[y];
    head[y] = tot;
  }
  int push_flow() {
    int x = en, flow = 168430090;
    while (x != st) {
      flow = min(flow, c[pre[x]]);
      x = b[pre[x] ^ 1];
    }
    x = en;
    while (x != st) {
      c[pre[x]] -= flow;
      c[pre[x] ^ 1] += flow;
      x = b[pre[x] ^ 1];
    }
    flowTot += flow;
    return flow * d[en];
  }
  int spfa() {
    memset(d, 10, sizeof(d));
    memset(p, 0, sizeof(p));
    q.push(st);
    p[st] = 1;
    d[st] = 0;
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      p[x] = 0;
      for (int go = head[x]; go; go = nxt[go])
        if (c[go] && d[b[go]] > d[x] + w[go]) {
          d[b[go]] = d[x] + w[go];
          pre[b[go]] = go;
          if (p[b[go]] == 0) {
            p[b[go]] = 1;
            q.push(b[go]);
          }
        }
    }
    return d[en] < 168430090;
  }
  int min_cost() {
    int ans = 0;
    while (spfa()) ans += push_flow();
    return ans;
  }
} mcmf;
int n;
int a[6015];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int src = 2 * n + 2, dst = 2 * n + 3;
  mcmf.init(2 * n + 3, src, dst);
  for (int i = 1; i <= n; i++) {
    int x1 = 0, x2 = 0, x3 = 0;
    for (int j = i + 1; j <= n; j++) {
      bool fg = 0;
      if (a[j] == a[i] + 1) {
        if (x1 >= 4) continue;
        x1++, fg = 1;
      } else if (a[j] == a[i] - 1) {
        if (x2 >= 4) continue;
        x2++, fg = 1;
      } else if ((a[j] - a[i]) % 7 == 0) {
        if (x3 >= 4) continue;
        x3++, fg = 1;
      }
      if (fg) mcmf.add(2 * i, 2 * j - 1, 1, 0);
    }
  }
  for (int i = 1; i <= n; i++) {
    mcmf.add(2 * i - 1, 2 * i, 1, -1);
    mcmf.add(0, 2 * i - 1, 1, 0);
    mcmf.add(2 * i, 2 * n + 1, 1, 0);
  }
  mcmf.add(src, 0, 4, 0);
  mcmf.add(2 * n + 1, dst, 4, 0);
  printf("%d\n", -mcmf.min_cost());
  return 0;
}