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 = 5000 + 20;
const int oo = 1e9 + 20;
const int MOD = 1e9 + 7;
template <class type>
struct mcmf {
  struct edge {
    int u, v, cap, flow;
    type cost;
  };
  int n;
  vector<edge> ed;
  vector<vector<int>> g;
  vector<int> p;
  vector<type> d, phi;
  mcmf(int n) : n(n), g(n), p(n), d(n), phi(n) {}
  void add_edge(int u, int v, int cap, type cost) {
    g[u].push_back(ed.size());
    ed.push_back({u, v, cap, 0, cost});
    g[v].push_back(ed.size());
    ed.push_back({v, u, 0, 0, -cost});
  }
  bool dijkstra(int s, int t) {
    fill(d.begin(), d.end(), oo);
    fill(p.begin(), p.end(), -1);
    set<pair<type, int>> q;
    d[s] = 0;
    for (q.insert({d[s], s}); q.size();) {
      int u = (*q.begin()).second;
      q.erase(q.begin());
      for (auto v : g[u]) {
        auto &e = ed[v];
        type nd = d[e.u] + e.cost + phi[e.u] - phi[e.v];
        if (0 < (e.cap - e.flow) && nd < d[e.v]) {
          q.erase({d[e.v], e.v});
          d[e.v] = nd;
          p[e.v] = v;
          q.insert({d[e.v], e.v});
        }
      }
    }
    for (int i = 0; i < n; i++) phi[i] = min(oo, phi[i] + d[i]);
    return d[t] != oo;
  }
  pair<int, type> max_flow(int s, int t) {
    type mc = 0;
    int mf = 0;
    fill(phi.begin(), phi.end(), 0);
    while (dijkstra(s, t)) {
      int flow = oo;
      for (int v = p[t]; v != -1; v = p[ed[v].u])
        flow = min(flow, ed[v].cap - ed[v].flow);
      for (int v = p[t]; v != -1; v = p[ed[v].u]) {
        edge &e1 = ed[v];
        edge &e2 = ed[v ^ 1];
        mc += e1.cost * flow;
        e1.flow += flow;
        e2.flow -= flow;
      }
      mf += flow;
    }
    return {mf, mc};
  }
};
int a[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  while (cin >> n) {
    int source = 2 * n, sink = source + 1, inter = sink + 1;
    mcmf<int> net(inter + 1);
    net.add_edge(source, inter, 2, 0);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      net.add_edge(i, i + n, 1, -1);
      net.add_edge(inter, i, 1, 0);
      net.add_edge(i + n, sink, 1, 0);
    }
    for (int i = 0; i < n; i++) {
      int may = 0, same = 0, men = 0;
      for (int j = i + 1; j < n; j++) {
        if (a[i] % 7 == a[j] % 7 && same < 20)
          net.add_edge(i + n, j, 1, 0), same++;
        if (a[i] + 1 == a[j] && may < 20) net.add_edge(i + n, j, 1, 0), may++;
        if (a[i] - 1 == a[j] && men < 20) net.add_edge(i + n, j, 1, 0), men++;
      }
    }
    cout << -net.max_flow(source, sink).second << '\n';
  }
  return 0;
}