#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
const int SINF = 0x7fffffff;
const long long LINF = 0x3fffffffffffffff;
const long long SLINF = 0x7fffffffffffffff;
const int MINF = 0x3f3f3f3f;
const int MAXN = 5007;
const int MAX = 7;
int n, kb;
int a[MAXN], m7[MAXN], sm[MAXN];
short dp[MAXN][MAXN];
short tc[MAXN][MAXN], m1[MAX][MAXN], m2[MAXN][MAX];
map<int, int> mp;
map<int, int>::iterator it;
void init();
void input();
void work();
void upd(int ni, int a, int b);
int main() {
  init();
  input();
  work();
}
void init() { ios::sync_with_stdio(false); }
void input() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
}
void work() {
  for (int i = 1; i <= n; ++i) mp[a[i]] = 0;
  kb = 0;
  for (auto &x : mp) x.second = ++kb;
  mp[0] = 0;
  for (int i = 1; i <= n; ++i) m7[i] = a[i] % 7, sm[i] = mp[a[i]];
  memset(dp, -MINF, sizeof(dp));
  memset(tc, -MINF, sizeof(tc));
  memset(m1, -MINF, sizeof(m1));
  memset(m2, -MINF, sizeof(m2));
  dp[0][0] = tc[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    int x = a[i];
    int x7 = x % 7;
    for (int j = 0; j < i; ++j)
      dp[i][j] =
          (((dp[i][j]) > (m1[x7][j] + 1)) ? (dp[i][j]) : (m1[x7][j] + 1)),
      dp[j][i] =
          (((dp[j][i]) > (m2[j][x7] + 1)) ? (dp[j][i]) : (m2[j][x7] + 1));
    for (int j = 0; j < i; ++j)
      dp[i][j] =
          (((dp[i][j]) > (tc[sm[j]][0] + 1)) ? (dp[i][j]) : (tc[sm[j]][0] + 1)),
      dp[j][i] =
          (((dp[j][i]) > (tc[sm[j]][0] + 1)) ? (dp[j][i]) : (tc[sm[j]][0] + 1));
    upd(i, x + 1, 0);
    upd(i, x - 1, 0);
    upd(i, 0, x + 1);
    upd(i, 0, x - 1);
    x = mp[x];
    for (int j = 0; j < n; ++j)
      tc[x][j] = (((tc[x][j]) > (dp[i][j])) ? (tc[x][j]) : (dp[i][j])),
      tc[sm[j]][i] =
          (((tc[sm[j]][i]) > (dp[j][i])) ? (tc[sm[j]][i]) : (dp[j][i]));
    for (int j = 0; j < n; ++j)
      m1[x7][j] = (((m1[x7][j]) > (dp[i][j])) ? (m1[x7][j]) : (dp[i][j])),
      m1[m7[j]][i] =
          (((m1[m7[j]][i]) > (dp[j][i])) ? (m1[m7[j]][i]) : (dp[j][i]));
    for (int j = 0; j < n; ++j)
      m2[j][x7] = (((m2[j][x7]) > (dp[j][i])) ? (m2[j][x7]) : (dp[j][i])),
      m2[i][m7[j]] =
          (((m2[i][m7[j]]) > (dp[i][j])) ? (m2[i][m7[j]]) : (dp[i][j]));
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      ans = (((ans) > (dp[i][j])) ? (ans) : (dp[i][j]));
  printf("%d\n", ans);
}
void upd(int ni, int a, int b) {
  if (!a && !b) return;
  int x7 = ::a[ni] % 7;
  if (!a) {
    if ((it = mp.find(b)) == mp.end()) return;
    b = it->second;
    for (int i = 0; i <= n; ++i)
      dp[i][ni] =
          (((dp[i][ni]) > (tc[b][i] + 1)) ? (dp[i][ni]) : (tc[b][i] + 1));
  } else {
    if ((it = mp.find(a)) == mp.end()) return;
    a = it->second;
    for (int i = 0; i <= n; ++i)
      dp[ni][i] =
          (((dp[ni][i]) > (tc[a][i] + 1)) ? (dp[ni][i]) : (tc[a][i] + 1));
  }
}
