#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int a[5005], dp[5005][5005], maxmod[8], maxnum[100010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%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]);
    }
  }
  printf("%d\n", ans);
}
