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 Inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
const int siz = 20005;
struct MinCostFlow {
  struct Edge {
    int to, cost, rev, id;
    int cap;
    Edge(int to, int cap, int cost, int rev, int id = 0)
        : to(to), cost(cost), rev(rev), id(id), cap(cap) {}
  };
  vector<Edge> egs[siz + 5];
  long long dis[siz + 5], h[siz + 5];
  int prv[siz + 5], pre[siz + 5];
  bool inq[siz + 5];
  bool foo = 0;
  void inline clear() {
    for (int i = 0; i < (siz + 5); ++i) egs[i].clear();
  }
  void inline addedge(int u, int v, int cap = INF, int cost = 0, int id = 0) {
    if (cost < 0) foo = 1;
    egs[u].push_back(Edge(v, cap, cost, ((int)(egs[v]).size()), id));
    egs[v].push_back(Edge(u, 0, -cost, ((int)(egs[u]).size()) - 1, -id));
  }
  void inline adduedge(int u, int v, int cap = INF, int cost = 0, int id = 0) {
    if (cost < 0) foo = 1;
    egs[u].push_back(Edge(v, cap, cost, ((int)(egs[v]).size()), id));
    egs[v].push_back(Edge(u, 0, -cost, ((int)(egs[u]).size()) - 1, -id));
    egs[v].push_back(Edge(u, cap, cost, ((int)(egs[u]).size()), id));
    egs[u].push_back(Edge(v, 0, -cost, ((int)(egs[v]).size()) - 1, -id));
  }
  bool inline spfa(int s, int t) {
    memset(dis, Inf, sizeof(dis));
    deque<int> q;
    dis[s] = 0;
    q.push_back(s);
    while (((int)(q).size())) {
      int x = q.front();
      q.pop_front();
      inq[x] = 0;
      for (int i = 0; i < (((int)(egs[x]).size())); ++i) {
        Edge &e = egs[x][i];
        if (e.cap && dis[e.to] > dis[x] + e.cost) {
          dis[e.to] = dis[x] + e.cost;
          prv[e.to] = x;
          pre[e.to] = i;
          if (!inq[e.to]) {
            if (((int)(q).size()) && dis[e.to] > dis[q.front()])
              q.push_back(e.to);
            else
              q.push_front(e.to);
            inq[e.to] = 1;
          }
        }
      }
    }
    memcpy(h, dis, sizeof(dis));
    foo = 0;
    return dis[t] < (INF >> 1);
  }
  bool inline dijkstra(int s, int t) {
    memset(dis, Inf, sizeof(dis));
    priority_queue<pair<int, int> > pq;
    dis[s] = 0;
    pq.push(make_pair(0, s));
    while (((int)(pq).size())) {
      pair<int, int> P = pq.top();
      pq.pop();
      int x = P.second, dist = -P.first;
      if (dis[x] != dist) continue;
      for (int i = 0; i < (((int)(egs[x]).size())); ++i) {
        Edge &e = egs[x][i];
        if (e.cap && dis[e.to] > dist + e.cost + h[x] - h[e.to]) {
          dis[e.to] = dist + e.cost + h[x] - h[e.to];
          prv[e.to] = x;
          pre[e.to] = i;
          pq.push(make_pair(-dis[e.to], e.to));
        }
      }
    }
    for (int i = 0; i < (siz + 5); ++i) {
      if (dis[i] < (INF >> 1))
        h[i] += dis[i];
      else
        h[i] = INF;
    }
    return dis[t] < (INF >> 1);
  }
  pair<long long, int> inline get(int s, int t, int f = INF) {
    long long res = 0;
    int F = 0;
    while (f) {
      if (foo ? !spfa(s, t) : !dijkstra(s, t)) break;
      int fl = f;
      for (int i = t; i != s; i = prv[i]) {
        fl = min(fl, egs[prv[i]][pre[i]].cap);
      }
      f -= fl;
      F += fl;
      res += 1ll * fl * h[t];
      for (int i = t; i != s; i = prv[i]) {
        Edge &e = egs[prv[i]][pre[i]];
        e.cap -= fl;
        egs[e.to][e.rev].cap += fl;
      }
    }
    return make_pair(res, F);
  }
} nf;
int n;
int a[3005];
int in[3005], ou[3005], t1[3005], t7[3005], nds;
int main() {
  scanf("%d", &n);
  int s = 0, t;
  for (int i = 1; i <= (n); ++i) {
    scanf("%d", a + i);
    in[i] = ++nds;
    ou[i] = ++nds;
    t1[i] = ++nds;
    t7[i] = ++nds;
    nf.addedge(in[i], ou[i], 1, -1);
  }
  t = ++nds;
  for (int i = 1; i <= (n); ++i) {
    nf.addedge(s, in[i], 1);
    nf.addedge(ou[i], t, 1);
    nf.addedge(t1[i], in[i], 1);
    nf.addedge(t7[i], in[i], 1);
    for (int j = (i + 1); j <= (n); ++j)
      if (a[i] % 7 == a[j] % 7) {
        nf.addedge(ou[i], t7[j], 1);
        nf.addedge(t7[i], t7[j]);
        break;
      }
    for (int j = (i + 1); j <= (n); ++j)
      if (a[i] + 1 == a[j]) {
        nf.addedge(ou[i], t1[j], 1);
        break;
      }
    for (int j = (i + 1); j <= (n); ++j)
      if (a[i] - 1 == a[j]) {
        nf.addedge(ou[i], t1[j], 1);
        break;
      }
    for (int j = (i + 1); j <= (n); ++j)
      if (a[i] == a[j]) {
        nf.addedge(t1[i], t1[j]);
        break;
      }
  }
  printf("%lld\n", -nf.get(s, t, 4).first);
  return 0;
}