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).
#include <bits/stdc++.h>
using namespace std;
inline int readInt() {
  int n = 0, ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) n = n * 10 + ch - '0', ch = getchar();
  return n;
}
const int MAX_N = 3000 + 3, MAX_VAL = 100000 + 3;
struct MCMF {
  static const int MAX_NODE = MAX_N * 4 + 2;
  struct Edge {
    int to, cost, cap, rev;
    Edge(int to, int cost, int cap, int rev)
        : to(to), cost(cost), cap(cap), rev(rev) {}
  };
  vector<Edge> G[MAX_NODE];
  int dis[MAX_NODE], prevE[MAX_NODE], prevV[MAX_NODE];
  bool inq[MAX_NODE];
  inline void addEdge(int u, int v, int cap, int w) {
    if (u == -1 || v == -1) return;
    G[u].push_back(Edge(v, w, cap, G[v].size()));
    G[v].push_back(Edge(u, -w, 0, G[u].size() - 1));
  }
  inline bool tension(int &a, const int &b) {
    return b < a ? (a = b, true) : false;
  }
  int minCostMaxFlow(int n, int s, int t, int lim) {
    int ans = 0;
    while (lim) {
      memset(dis, 0x3f, sizeof dis);
      memset(inq, 0, sizeof inq);
      static int q[MAX_NODE];
      int l = 0, r = 0;
      q[r++] = s, dis[s] = 0, inq[s] = 1;
      while (r - l >= 1) {
        int u = q[(l++ + n) % n];
        inq[u] = false;
        for (int i = 0; i < (int)G[u].size(); ++i) {
          Edge &e = G[u][i];
          if (e.cap && tension(dis[e.to], dis[u] + e.cost)) {
            prevV[e.to] = u;
            prevE[e.to] = i;
            if (!inq[e.to]) {
              if (r - l >= 1 && dis[q[(l + n) % n]] > dis[e.to])
                q[(--l + n) % n] = e.to;
              else
                q[(r++) % n] = e.to;
              inq[e.to] = true;
            }
          }
        }
      }
      if (dis[t] == 0x3f3f3f3f) return -1;
      int flow = lim;
      for (int i = t; i != s; i = prevV[i])
        flow = min(flow, G[prevV[i]][prevE[i]].cap);
      ans += flow * dis[t];
      lim -= flow;
      for (int i = t; i != s; i = prevV[i]) {
        Edge &e = G[prevV[i]][prevE[i]];
        e.cap -= flow;
        G[e.to][e.rev].cap += flow;
      }
    }
    return ans;
  }
} mcmf;
int n, a[MAX_N], s1[7], s2[MAX_VAL];
int main() {
  n = readInt();
  int s = n * 2, t = s + 1, cnt = t + 1;
  memset(s1, -1, sizeof s1);
  memset(s2, -1, sizeof s2);
  for (int i = 0; i < n; ++i) {
    a[i] = readInt();
    mcmf.addEdge(i, i + n, 1, -1);
    mcmf.addEdge(s, i, 1, 0);
    mcmf.addEdge(i + n, t, 1, 0);
    mcmf.addEdge(s1[a[i] % 7], i, 1, 0);
    mcmf.addEdge(s2[a[i] - 1], i, 1, 0);
    mcmf.addEdge(s2[a[i] + 1], i, 1, 0);
    mcmf.addEdge(i + n, cnt, 1, 0);
    mcmf.addEdge(s1[a[i] % 7], cnt, 1, 0);
    s1[a[i] % 7] = cnt++;
    mcmf.addEdge(i + n, cnt, 1, 0);
    mcmf.addEdge(s2[a[i]], cnt, 1, 0);
    s2[a[i]] = cnt++;
  }
  printf("%d\n", -mcmf.minCostMaxFlow(cnt, s, t, 4));
  return 0;
}
