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;
long long read() {
  long long rtn = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch <= '9' && ch >= '0')
    rtn = (rtn << 3) + (rtn << 1) + ch - '0', ch = getchar();
  return rtn;
}
const int N = 100010;
const long long mod = 1e9 + 7;
int n;
struct edge {
  int b, id, f, w;
  edge(int bb = 0, int iid = 0, int ff = 0, int ww = 0) {
    b = bb;
    id = iid;
    f = ff;
    w = ww;
  }
};
vector<int> g[6010];
vector<int> w[6010];
bool f[6010][6010];
int dis[6010], inq[6010], pre1[6010], pre2[6010];
int S, T;
void spfa() {
  deque<int> q;
  for (int i = 1; i <= T; i++) dis[i] = 1e9;
  q.push_back(S);
  dis[S] = 0;
  inq[S] = 1;
  while (!q.empty()) {
    int k = q.front();
    q.pop_front();
    inq[k] = 0;
    for (int i = 0, sz = g[k].size(); i < sz; i++) {
      int t = g[k][i], ww = w[k][i];
      if (dis[k] + ww < dis[t] && f[k][t]) {
        dis[t] = dis[k] + ww;
        pre1[t] = i;
        pre2[t] = k;
        if (!inq[t]) {
          if (q.empty() || dis[q.front()] > dis[t])
            q.push_front(t);
          else
            q.push_back(t);
          inq[t] = 1;
        }
      }
    }
  }
}
int mcf() {
  int rtn = 0;
  for (int i = 1; i <= 4; i++) {
    spfa();
    for (int j = T; pre2[j]; j = pre2[j]) {
      int s = pre2[j], k = pre1[j];
      f[s][j] = 0;
      f[j][s] = 1;
      rtn += w[s][k];
    }
  }
  return rtn;
}
int a[3010];
void anode(int x, int y, int z, int ww) {
  g[x].push_back(y);
  g[y].push_back(x);
  w[x].push_back(ww);
  w[y].push_back(-ww);
  f[x][y] = z;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  S = 2 * n + 1;
  T = S + 1;
  for (int i = 1; i <= n; i++) {
    anode(S, i, 1, 0);
    anode(i + n, T, 1, 0);
    anode(i, i + n, 1, -1);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if ((a[j] - a[i] == 1) || (a[i] - a[j] == 1) || ((a[i] - a[j]) % 7 == 0))
        anode(i + n, j, 1, 0);
    }
  }
  int ans = mcf();
  printf("%d\n", -ans);
  return 0;
}