#include <bits/stdc++.h>
int n, ans, f[5005], dp[5005][5005], p[100005], q[100005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &f[i]);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= i; j++) dp[i][j] = -1;
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= n; j++) p[f[j]] = q[f[j] % 7] = 0;
    for (int j = 1; j < n; j++)
      p[f[j]] = std::max(p[f[j]], dp[i][j]),
      q[f[j] % 7] = std::max(q[f[j] % 7], dp[i][j]);
    for (int j = i + 1; j <= n; j++)
      dp[j][i] = dp[i][j] =
          std::max(dp[i][0],
                   std::max(q[f[j] % 7], std::max(p[f[j] - 1], p[f[j] + 1]))) +
          1,
      p[f[j]] = std::max(p[f[j]], dp[i][j]),
      q[f[j] % 7] = std::max(q[f[j] % 7], dp[i][j]);
  }
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= i; j++) ans = std::max(ans, dp[i][j]);
  printf("%d\n", ans);
  return 0;
}
