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;
const int N = 6005;
const int M = 100000;
const int inf = 1e9;
struct Edge {
  int v, w, cost, next;
} g[M];
int head[N], st, ed, flow, cost, cnt;
void init() {
  memset(head, -1, sizeof(head));
  flow = cost = cnt = 0;
}
void addEdge(int u, int v, int w, int cost) {
  g[cnt].v = v, g[cnt].w = w, g[cnt].cost = cost, g[cnt].next = head[u],
  head[u] = cnt++;
  g[cnt].v = u, g[cnt].w = 0, g[cnt].cost = -cost, g[cnt].next = head[v],
  head[v] = cnt++;
}
bool spfaFlow(int n) {
  vector<int> dis(n + 1, inf);
  vector<bool> vis(n + 1, false);
  vector<int> path(n + 1, -1);
  dis[st] = 0;
  vis[st] = true;
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; ~i; i = g[i].next) {
      int v = g[i].v;
      if (g[i].w && dis[v] > dis[u] + g[i].cost) {
        dis[v] = dis[u] + g[i].cost;
        path[v] = i;
        if (!vis[v]) {
          vis[v] = true;
          q.push(v);
        }
      }
    }
  }
  int mxflow = inf;
  if (path[ed] != -1) {
    for (int u = ed; u != st; u = g[path[u] ^ 1].v) {
      mxflow = min(mxflow, g[path[u]].w);
    }
    flow += mxflow;
    for (int u = ed; u != st; u = g[path[u] ^ 1].v) {
      g[path[u]].w -= mxflow;
      g[path[u] ^ 1].w += mxflow;
      cost += mxflow * g[path[u]].cost;
    }
  }
  return path[ed] != -1;
}
int minCost(int n) {
  while (spfaFlow(n))
    ;
  return cost;
}
int main(int argc, char *argv[]) {
  cin.sync_with_stdio(false);
  init();
  int n;
  cin >> n;
  vector<int> a(n + 1);
  vector<deque<int>> b(100002), c(7);
  st = 2 * n + 1, ed = 2 * n + 3;
  int sed = 2 * n + 2;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]].push_back(i);
    c[a[i] % 7].push_back(i);
    addEdge(st, i, 1, 0);
    addEdge(i, i + n, 1, -1);
    addEdge(i + n, sed, 1, 0);
  }
  for (int i = 1; i <= n; i++) {
    b[a[i]].pop_front();
    c[a[i] % 7].pop_front();
    int cnt = 4;
    for (auto x : b[a[i] - 1]) {
      addEdge(i + n, x, 1, 0);
      if (--cnt == 0) break;
    }
    cnt = 4;
    for (auto x : b[a[i] + 1]) {
      addEdge(i + n, x, 1, 0);
      if (--cnt == 0) break;
    }
    cnt = 4;
    for (auto x : c[a[i] % 7]) {
      addEdge(i + n, x, 1, 0);
      if (--cnt == 0) break;
    }
  }
  addEdge(sed, ed, 4, 0);
  cout << -minCost(2 * n + 3) << endl;
  return 0;
}