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 <typename T>
void unique(T &c) {
  c.erase(std::unique(c.begin(), c.end()), c.end());
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 != vec.size()) os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end()) os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end()) os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end()) os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end()) os << " ";
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    os << "(" << itr->first << ", " << itr->second << ")";
    auto titr = itr;
    if (++titr != mp.end()) os << " ";
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    os << "(" << itr->first << ", " << itr->second << ")";
    auto titr = itr;
    if (++titr != mp.end()) os << " ";
  }
  return os;
}
using ll = long long int;
using P = pair<int, int>;
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
struct fast_io {
  fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(20);
  }
} fast_io_;
template <typename T>
struct edge {
  int to;
  T cap;
  T cost;
  int rev;
};
template <typename T>
struct Graph {
  int n;
  vector<vector<edge<T>>> vec;
  Graph(int n) : n(n) { vec.resize(n); }
  void adde(int at, int to, T cap, T cost) {
    vec[at].push_back((edge<T>){to, cap, cost, (int)vec[to].size()});
    vec[to].push_back((edge<T>){at, 0, -cost, (int)vec[at].size() - 1});
  }
  T min_cost_flow(int s, int t, T f) {
    T res = 0;
    vector<T> h(n, 0), dis(n, 0);
    vector<int> prevv(n), preve(n);
    T inf = numeric_limits<T>::max();
    while (f > 0) {
      priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>>
          pq;
      fill(dis.begin(), dis.end(), inf);
      dis[s] = 0;
      pq.push(make_pair(0, s));
      while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();
        int v = p.second;
        if (dis[v] < p.first) continue;
        for (int i = 0; i < vec[v].size(); i++) {
          auto &e = vec[v][i];
          if (e.cap > 0 && dis[e.to] > dis[v] + e.cost + h[v] - h[e.to]) {
            dis[e.to] = dis[v] + e.cost + h[v] - h[e.to];
            prevv[e.to] = v;
            preve[e.to] = i;
            pq.push(make_pair(dis[e.to], e.to));
          }
        }
      }
      if (dis[t] == inf) return -1;
      for (int i = 0; i < n; i++) h[i] += dis[i];
      T d = f;
      for (int i = t; i != s; i = prevv[i]) {
        d = min(d, vec[prevv[i]][preve[i]].cap);
      }
      f -= d;
      res += d * h[t];
      for (int i = t; i != s; i = prevv[i]) {
        auto &e = vec[prevv[i]][preve[i]];
        e.cap -= d;
        vec[i][e.rev].cap += d;
      }
    }
    return res;
  }
};
using GraphI = Graph<int>;
using GraphL = Graph<long long>;
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<int> a(n);
  cin >> a;
  GraphI g(2 * n + 2);
  for (int i = (0); i < (n); ++i) {
    g.adde(0, i + 1, 1, 0);
    g.adde(i + 1, n + i + 1, 1, -1);
    g.adde(n + i + 1, 2 * n + 1, 1, 0);
    for (int j = (i + 1); j < (n); ++j) {
      if (abs(a[i] - a[j]) == 1 || a[i] % 7 == a[j] % 7) {
        g.adde(n + i + 1, j + 1, 1, 0);
      }
    }
  }
  cout << -g.min_cost_flow(0, 2 * n + 1, 4) << endl;
  return 0;
}