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>
#pragma GCC optimize("O3")
using namespace std;
inline int getint() {
  int _x = 0;
  char _tc = getchar();
  while (_tc < '0' || _tc > '9') _tc = getchar();
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x;
}
struct MinCostMaxFlow {
  static const int MAXV = 10010;
  static const int INF = 1000000;
  struct Edge {
    int v, cap, w, rev;
    Edge() {}
    Edge(int t2, int t3, int t4, int t5) : v(t2), cap(t3), w(t4), rev(t5) {}
  };
  int V, s, t;
  vector<Edge> g[MAXV];
  void init(int n) {
    V = n + 2;
    s = n + 1, t = n + 2;
    for (int i = 1; i <= V; i++) g[i].clear();
  }
  void addEdge(int a, int b, int cap, int w) {
    g[a].push_back(Edge(b, cap, w, (int)g[b].size()));
    g[b].push_back(Edge(a, 0, -w, (int)g[a].size() - 1));
  }
  int d[MAXV], id[MAXV], mom[MAXV];
  bool inqu[MAXV];
  int solve() {
    int mxf = 0, mnc = 0;
    while (1) {
      fill(d + 1, d + 1 + V, INF);
      fill(inqu + 1, inqu + 1 + V, 0);
      fill(mom + 1, mom + 1 + V, -1);
      mom[s] = s;
      d[s] = 0;
      queue<int> Q;
      Q.push(s);
      inqu[s] = 1;
      while (not Q.empty()) {
        int u = Q.front();
        Q.pop();
        inqu[u] = 0;
        for (int i = 0; i < (int)g[u].size(); i++) {
          Edge &e = g[u][i];
          int v = e.v;
          if (e.cap > 0 && d[v] > d[u] + e.w) {
            d[v] = d[u] + e.w;
            mom[v] = u;
            id[v] = i;
            if (!inqu[v]) Q.push(v), inqu[v] = 1;
          }
        }
      }
      if (mom[t] == -1) break;
      int df = INF;
      for (int u = t; u != s; u = mom[u]) df = min(df, g[mom[u]][id[u]].cap);
      for (int u = t; u != s; u = mom[u]) {
        Edge &e = g[mom[u]][id[u]];
        e.cap -= df;
        g[e.v][e.rev].cap += df;
      }
      mxf += df;
      mnc += df * d[t];
    }
    return mnc;
  }
} flow;
int n, a[3030];
void init() {
  n = getint();
  for (int i = 1; i <= n; i++) {
    a[i] = getint();
  }
}
void solve() {
  flow.init(n + n + 1);
  int s = n + n + 1;
  flow.addEdge(flow.s, s, 4, 0);
  for (int i = 1; i <= n; i++) {
    flow.addEdge(s, i, 1, 0);
    flow.addEdge(i, i + n, 1, -1);
    flow.addEdge(i + n, flow.t, 1, 0);
    int con = 0;
    int neg = 0;
    int pos = 0;
    for (int j = i + 1; j <= n; j++) {
      if (a[j] - a[i] == -1 and neg < 10) {
        neg++;
        flow.addEdge(i + n, j, 1, 0);
      }
      if (a[j] - a[i] == +1 and pos < 10) {
        pos++;
        flow.addEdge(i + n, j, 1, 0);
      }
      if (a[j] % 7 == a[i] % 7 and con < 10) {
        con++;
        flow.addEdge(i + n, j, 1, 0);
      }
    }
  }
  cout << -flow.solve() << endl;
}
int main() {
  init();
  solve();
}