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;
string DBG_DLM(int& i) { return (i++ == 0 ? "" : ", "); }
template <class T>
ostream& operator<<(ostream& os, vector<T> v);
template <class T>
ostream& operator<<(ostream& os, set<T> v);
template <class T>
ostream& operator<<(ostream& os, queue<T> q);
template <class T>
ostream& operator<<(ostream& os, priority_queue<T> q);
template <class T, class K>
ostream& operator<<(ostream& os, pair<T, K> p);
template <class T, class K>
ostream& operator<<(ostream& os, map<T, K> mp);
template <class T, class K>
ostream& operator<<(ostream& os, unordered_map<T, K> mp);
template <int I, class TPL>
void DBG(ostream& os, TPL t) {}
template <int I, class TPL, class H, class... Ts>
void DBG(ostream& os, TPL t) {
  os << (I == 0 ? "" : ", ") << get<I>(t);
  DBG<I + 1, TPL, Ts...>(os, t);
}
template <class T, class K>
void DBG(ostream& os, pair<T, K> p, string delim) {
  os << "(" << p.first << delim << p.second << ")";
}
template <class... Ts>
ostream& operator<<(ostream& os, tuple<Ts...> t) {
  os << "(";
  DBG<0, tuple<Ts...>, Ts...>(os, t);
  os << ")";
  return os;
}
template <class T, class K>
ostream& operator<<(ostream& os, pair<T, K> p) {
  DBG(os, p, ", ");
  return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
  {
    int i = 0;
    os << "{";
    {
      for (auto&& t : v) {
        os << DBG_DLM(i) << t;
      };
    }
    os << "}";
    return os;
  };
}
template <class T>
ostream& operator<<(ostream& os, set<T> s) {
  {
    int i = 0;
    os << "{";
    {
      for (auto&& t : s) {
        os << DBG_DLM(i) << t;
      };
    }
    os << "}";
    return os;
  };
}
template <class T>
ostream& operator<<(ostream& os, queue<T> q) {
  {
    int i = 0;
    os << "{";
    {
      for (; q.size(); q.pop()) {
        os << DBG_DLM(i) << q.front();
      };
    }
    os << "}";
    return os;
  };
}
template <class T>
ostream& operator<<(ostream& os, priority_queue<T> q) {
  {
    int i = 0;
    os << "{";
    {
      for (; q.size(); q.pop()) {
        os << DBG_DLM(i) << q.top();
      };
    }
    os << "}";
    return os;
  };
}
template <class T, class K>
ostream& operator<<(ostream& os, map<T, K> m) {
  {
    int i = 0;
    os << "{";
    {
      for (auto&& p : m) {
        os << DBG_DLM(i);
        DBG(os, p, "->");
      };
    }
    os << "}";
    return os;
  };
}
template <class T, class K>
ostream& operator<<(ostream& os, unordered_map<T, K> m) {
  {
    int i = 0;
    os << "{";
    {
      for (auto&& p : m) {
        os << DBG_DLM(i);
        DBG(os, p, "->");
      };
    }
    os << "}";
    return os;
  };
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
using vvvpii = vector<vvpii>;
template <class T>
bool amax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
bool amin(T& a, const T& b) {
  return a > b ? a = b, 1 : 0;
}
ll ri() {
  ll l;
  cin >> l;
  return l;
}
string rs() {
  string s;
  cin >> s;
  return s;
}
void Main() {
  int n = ri();
  vector<int> A(n);
  for (int i = int(0); i < int(n); ++i) A[i] = ri();
  int ma = *max_element((A).begin(), (A).end());
  vvi M(7);
  vvi N(ma + 2);
  for (int i = int(0); i < int(n); ++i) {
    int a = A[i];
    int a7 = a % 7;
    N[a].emplace_back(i + 1);
    M[a7].emplace_back(i + 1);
  }
  vvi dp(n + 1, vi(n));
  for (int i = int(0); i < int(n); ++i) dp[i + 1][0] = 1;
  vi P(ma + 2);
  vi Q(7);
  for (int i = int(1); i < int(n); ++i) {
    int a = A[i - 1];
    int a7 = a % 7;
    P[a]++;
    Q[a7]++;
    for (int j = int(0); j < int(i); ++j)
      if (dp[i][j]) {
        if (P[a + 1] < ((int)(N[a + 1]).size()))
          amax(dp[N[a + 1][P[a + 1]]][j], dp[i][j] + 1);
        if (P[a - 1] < ((int)(N[a - 1]).size()))
          amax(dp[N[a - 1][P[a - 1]]][j], dp[i][j] + 1);
        if (Q[a7] < ((int)(M[a7]).size()))
          amax(dp[M[a7][Q[a7]]][j], dp[i][j] + 1);
        if (j == 0) {
          for (int k = int(i); k < int(n); ++k)
            amax(dp[k + 1][i], dp[i][j] + 1);
        } else {
          int b = A[j - 1];
          int b7 = b % 7;
          if (P[b + 1] < ((int)(N[b + 1]).size()))
            amax(dp[N[b + 1][P[b + 1]]][i], dp[i][j] + 1);
          if (P[b - 1] < ((int)(N[b - 1]).size()))
            amax(dp[N[b - 1][P[b - 1]]][i], dp[i][j] + 1);
          if (Q[b7] < ((int)(M[b7]).size()))
            amax(dp[M[b7][Q[b7]]][i], dp[i][j] + 1);
        }
      }
  }
  int ans = 0;
  for (int i = int(1); i < int(n + 1); ++i)
    for (int j = int(0); j < int(i); ++j) {
      amax(ans, dp[i][j]);
    }
  cout << ans << endl;
}
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  Main();
  return 0;
}