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;
const int N = 7000;
const long long INF = (long long)1e10;
struct Edge {
  int v, idr;
  long long w, c;
};
vector<Edge> gr[N];
int ss = N - 2, tt = N - 1, pr[N], ide[N];
long long dis[N], phi[N];
bool dijkstra() {
  for (int i = 0; i < N; ++i) dis[i] = INF;
  dis[ss] = 0;
  priority_queue<pair<long long, int> > pq;
  pq.push({0LL, ss});
  while (!pq.empty()) {
    int u = pq.top().second;
    long long pd = -pq.top().first;
    pq.pop();
    if (dis[u] != pd) continue;
    for (int i = 0; i < gr[u].size(); ++i) {
      Edge &e = gr[u][i];
      long long nd = pd + e.c + phi[u] - phi[e.v];
      if (e.w && nd < dis[e.v]) {
        pr[e.v] = u, ide[e.v] = i;
        dis[e.v] = nd;
        pq.push({-nd, e.v});
      }
    }
  }
  for (int i = 0; i < N; ++i) phi[i] = min(INF, phi[i] + dis[i]);
  return dis[tt] != INF;
}
void add(int u, int v, long long w, long long c) {
  gr[u].push_back({v, int(gr[v].size()), w, c});
  gr[v].push_back({u, int(gr[u].size() - 1), 0LL, -c});
}
pair<long long, long long> mfmc() {
  long long mf = 0;
  long long mc = 0;
  while (dijkstra()) {
    long long cf = INF;
    for (int v = tt; v != ss; v = pr[v]) cf = min(cf, gr[pr[v]][ide[v]].w);
    if (cf == INF) return {INF, INF};
    for (int v = tt; v != ss; v = pr[v]) {
      auto &e = gr[pr[v]][ide[v]];
      auto &er = gr[v][e.idr];
      e.w -= cf;
      er.w += cf;
      mc += cf * e.c;
    }
    mf += cf;
  }
  return {mf, mc};
}
int arr[N], len[N];
vector<int> aux[N];
int dfs(int idx) {
  int &r = len[idx];
  if (r == -1) {
    r = 0;
    for (int y : aux[idx]) r = max(r, dfs(y));
    ++r;
  }
  return r;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", arr + i);
  int s1 = N - 3;
  add(ss, s1, 4, 0);
  for (int i = 0; i < n; ++i) {
    add(s1, i, 1, 0);
    for (int j = i + 1; j < n; ++j)
      if (abs(arr[j] - arr[i]) == 1 || (arr[j] % 7 == arr[i] % 7)) {
        aux[j].push_back(i);
        add(n + i, j, 1, 0);
      }
    add(i, n + i, 1, -1);
    add(n + i, tt, 1, 0);
  }
  memset(len, -1, sizeof len);
  for (int i = 0; i < n; ++i) {
    phi[n + i] = -dfs(i);
    phi[i] = phi[n + i] + 1;
    phi[tt] = min(phi[tt], phi[n + i]);
  }
  printf("%lld\n", -mfmc().second);
  return 0;
}
