#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int a[5005], dp[5005][5005], maxmod[8], maxnum[100010];
inline bool scan_d(int &num) {
  char in;
  bool IsN = false;
  in = getchar();
  if (in == EOF) return false;
  while (in != '-' && (in < '0' || in > '9')) in = getchar();
  if (in == '-') {
    IsN = true;
    num = 0;
  } else
    num = in - '0';
  while (in = getchar(), in >= '0' && in <= '9') {
    num *= 10, num += in - '0';
  }
  if (IsN) num = -num;
  return true;
}
void print_d(int a) {
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a >= 10) {
    print_d(a / 10);
  }
  putchar(a % 10 + '0');
}
int main() {
  int n;
  scan_d(n);
  for (int i = 1; i <= n; i++) scan_d(a[i]);
  memset(dp, 0, sizeof dp);
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    memset(maxmod, 0, sizeof maxmod);
    memset(maxnum, 0, sizeof maxnum);
    for (int j = 1; j <= i; j++) {
      maxmod[a[j] % 7] = max(maxmod[a[j] % 7], dp[i][j]);
      maxnum[a[j]] = max(maxnum[a[j]], dp[i][j]);
    }
    for (int j = i + 1; j <= n; j++) {
      dp[i][j] = max(dp[i][j], dp[i][0] + 1);
      dp[i][j] = max(dp[i][j], maxmod[a[j] % 7] + 1);
      dp[i][j] = max(dp[i][j], maxnum[a[j] - 1] + 1);
      dp[i][j] = max(dp[i][j], maxnum[a[j] + 1] + 1);
      maxmod[a[j] % 7] = max(maxmod[a[j] % 7], dp[i][j]);
      maxnum[a[j]] = max(maxnum[a[j]], dp[i][j]);
      dp[j][i] = dp[i][j];
      ans = max(ans, dp[i][j]);
    }
  }
  print_d(ans);
  puts("");
  return 0;
}
