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;
namespace mcmf {
const int MAX = 70507;
const long long INF = 1LL << 60;
long long cap[MAX], flow[MAX], cost[MAX], dis[MAX];
int n, m, s, t, Q[1000007], adj[MAX], link[MAX], last[MAX], from[MAX],
    visited[MAX];
void init(int nodes, int source, int sink) {
  m = 0, n = nodes, s = source, t = sink;
  for (int i = 0; i <= n; i++) last[i] = -1;
}
void addEdge(int u, int v, long long c, long long w) {
  adj[m] = v, cap[m] = c, flow[m] = 0, cost[m] = +w, link[m] = last[u],
  last[u] = m++;
  adj[m] = u, cap[m] = 0, flow[m] = 0, cost[m] = -w, link[m] = last[v],
  last[v] = m++;
}
bool spfa() {
  int i, j, x, f = 0, l = 0;
  for (i = 0; i <= n; i++) visited[i] = 0, dis[i] = INF;
  dis[s] = 0, Q[l++] = s;
  while (f < l) {
    i = Q[f++];
    for (j = last[i]; j != -1; j = link[j]) {
      if (flow[j] < cap[j]) {
        x = adj[j];
        if (dis[x] > dis[i] + cost[j]) {
          dis[x] = dis[i] + cost[j], from[x] = j;
          if (!visited[x]) {
            visited[x] = 1;
            if (f && rand() & 7)
              Q[--f] = x;
            else
              Q[l++] = x;
          }
        }
      }
    }
    visited[i] = 0;
  }
  return (dis[t] != INF);
}
pair<long long, long long> solve() {
  int i, j;
  long long mincost = 0, maxflow = 0;
  while (maxflow < 4 and spfa()) {
    long long aug = INF;
    for (i = t, j = from[i]; i != s; i = adj[j ^ 1], j = from[i]) {
      aug = min(aug, cap[j] - flow[j]);
    }
    for (i = t, j = from[i]; i != s; i = adj[j ^ 1], j = from[i]) {
      flow[j] += aug, flow[j ^ 1] -= aug;
    }
    maxflow += aug, mincost += aug * dis[t];
  }
  return make_pair(mincost, maxflow);
}
}  // namespace mcmf
using namespace mcmf;
int arr[5007];
int next_same[100007], next_same_mod[10];
int main() {
  int n;
  scanf("%d", &n);
  int src = 4 * n + 2, sink = 4 * n + 3;
  init(4 * n + 7, src, sink);
  for (int i = 1; i <= n; i++) scanf("%d", &arr[i]);
  for (int i = 1; i <= 100000; i++) next_same[i] = sink;
  for (int i = 0; i < 7; i++) next_same_mod[i] = sink;
  for (int i = 1; i <= n; i++) {
    int to = next_same_mod[arr[i] % 7];
    addEdge(i + 2 * n, to, INF, 0);
    addEdge(i + 2 * n, i, 1, 0);
    addEdge(i + n, to, 1, 0);
    next_same_mod[arr[i] % 7] = i + 2 * n;
    to = next_same[arr[i]];
    addEdge(i + 3 * n, to, INF, 0);
    addEdge(i + 3 * n, i, 1, 0);
    addEdge(i + n, next_same[arr[i] + 1], 1, 0);
    addEdge(i + n, next_same[arr[i] - 1], 1, 0);
    next_same[arr[i]] = i + 3 * n;
  }
  for (int i = 1; i <= n; i++)
    addEdge(src, i, 1, 0), addEdge(i, i + n, 1, -1), addEdge(i + n, sink, 1, 0);
  long long ans = solve().first;
  printf("%lld\n", abs(ans));
}
