#include <bits/stdc++.h>
using namespace std;
int dp[5000][5001], n, mxa[7], mxb[100002], x[5001];
int main() {
  scanf("%d", &n);
  for (int i = 1; i < (int)(n + 1); ++i) scanf("%d", x + i);
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(7); ++j) mxa[j] = 0;
    for (int j = 0; j < (int)(n + 1); ++j) mxb[x[j]] = 0;
    for (int j = 1; j < (int)(i); ++j)
      mxa[x[j] % 7] = max(mxa[x[j] % 7], dp[j][i]),
                 mxb[x[j]] = max(mxb[x[j]], dp[j][i]);
    for (int j = i + 1; j < (int)(n + 1); ++j) {
      dp[i][j] =
          max(mxa[x[j] % 7], max(mxb[x[j] + 1], max(mxb[x[j] - 1], dp[0][i]))) +
          1;
      mxa[x[j] % 7] = dp[i][j];
      mxb[x[j]] = max(mxb[x[j]], dp[i][j]);
    }
  }
  int a = 0;
  for (int i = 1; i < (int)(n); ++i)
    for (int j = i + 1; j < (int)(n + 1); ++j) a = max(a, dp[i][j]);
  printf("%d\n", a);
}
