#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 5005;
long long a[N], dp[N][N], ans, maxnum[100005], maxmod[8];
void solve() {
  long long n;
  cin >> n;
  for (long long x = 1; x < n + 1; x++) {
    cin >> a[x];
  }
  for (long long y = 0; y < n; y++) {
    for (long long i = 1; i < n + 1; i++) {
      maxnum[a[i]] = 0;
    }
    for (long long i = 0; i < 7; i++) {
      maxmod[i] = 0;
    }
    for (long long x = 1; x < n + 1; x++) {
      if (x == y) continue;
      if (x > y) {
        dp[x][y] = dp[y][x] = 1 + max({maxmod[a[x] % 7], maxnum[a[x] + 1],
                                       maxnum[a[x] - 1], dp[y][0]});
        amax(ans, dp[x][y]);
      }
      amax(maxnum[a[x]], dp[x][y]);
      amax(maxmod[a[x] % 7], dp[x][y]);
    }
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
