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;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << '{';
  for (int i = (0), __i = (((int)(vec).size())); i < __i; i++) {
    os << vec[i];
    if (i + 1 != ((int)(vec).size())) os << ',';
  }
  os << '}';
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &par) {
  os << '(' << par.first << ',' << par.second << ')';
  return os;
}
int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
template <class T>
T sqr(T x) {
  return x * x;
}
const int INF = 1000 * 1000 * 1000;
struct rib {
  int b, u, c, f;
  size_t back;
};
vector<vector<rib>> g(3 * 3030);
void add_rib(int a, int b, int u, int c) {
  rib r1 = {b, u, c, 0, g[b].size()};
  rib r2 = {a, 0, -c, 0, g[a].size()};
  g[a].push_back(r1);
  g[b].push_back(r2);
}
int a[3030];
int lastnum[101010];
int lastmod[7];
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = (0), __i = (n); i < __i; i++) cin >> a[i + 1];
  int N = 3 * n + 2;
  for (int i = (0), __i = (101010); i < __i; i++) lastnum[i] = -1;
  for (int i = (0), __i = (7); i < __i; i++) lastmod[i] = -1;
  for (int i = (0), __i = (n); i < __i; i++) {
    add_rib(0, 3 * i + 1, 1, 0);
    add_rib(3 * i + 1, 3 * i + 2, 1, 0);
    add_rib(3 * i + 2, 3 * i + 3, 1, -1);
    add_rib(3 * i + 3, 3 * n + 1, 1, 0);
    if (lastnum[a[n - i] - 1] != -1)
      add_rib(3 * (n - i - 1) + 3, 3 * (lastnum[a[n - i] - 1] - 1) + 2, 1, 0);
    if (lastnum[a[n - i] + 1] != -1)
      add_rib(3 * (n - i - 1) + 3, 3 * (lastnum[a[n - i] + 1] - 1) + 2, 1, 0);
    if (lastmod[a[n - i] % 7] != -1)
      add_rib(3 * (n - i - 1) + 3, 3 * (lastmod[a[n - i] % 7] - 1) + 1, 1, 0);
    if (lastnum[a[n - i]] != -1)
      add_rib(3 * (n - i - 1) + 2, 3 * (lastnum[a[n - i]] - 1) + 2, 4, 0);
    if (lastmod[a[n - i] % 7] != -1)
      add_rib(3 * (n - i - 1) + 1, 3 * (lastmod[a[n - i] % 7] - 1) + 1, 4, 0);
    lastnum[a[n - i]] = n - i;
    lastmod[a[n - i] % 7] = n - i;
  }
  int s = 0, t = 3 * n + 1;
  int flow = 0, cost = 0;
  const int INF = 1000000000;
  while (flow < 4) {
    vector<int> id(N, 0);
    vector<int> d(N, INF);
    vector<int> q(N);
    vector<int> p(N);
    vector<size_t> p_rib(N);
    int qh = 0, qt = 0;
    q[qt++] = s;
    d[s] = 0;
    while (qh != qt) {
      int v = q[qh++];
      id[v] = 2;
      if (qh == N) qh = 0;
      for (size_t i = 0; i < g[v].size(); ++i) {
        rib &r = g[v][i];
        if (r.f < r.u && d[v] + r.c < d[r.b]) {
          d[r.b] = d[v] + r.c;
          if (id[r.b] == 0) {
            q[qt++] = r.b;
            if (qt == N) qt = 0;
          } else if (id[r.b] == 2) {
            if (--qh == -1) qh = N - 1;
            q[qh] = r.b;
          }
          id[r.b] = 1;
          p[r.b] = v;
          p_rib[r.b] = i;
        }
      }
    }
    if (d[t] == INF) break;
    int addflow = 4 - flow;
    for (int v = t; v != s; v = p[v]) {
      int pv = p[v];
      size_t pr = p_rib[v];
      addflow = min(addflow, g[pv][pr].u - g[pv][pr].f);
    }
    for (int v = t; v != s; v = p[v]) {
      int pv = p[v];
      size_t pr = p_rib[v], r = g[pv][pr].back;
      g[pv][pr].f += addflow;
      g[v][r].f -= addflow;
      cost += g[pv][pr].c * addflow;
    }
    flow += addflow;
  }
  cout << -cost << endl;
  return 0;
}