#include <bits/stdc++.h>
using namespace std;
int dp[5010][5010];
int a[5010], pre[100010], mod[8];
int main() {
  int n, ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0; i <= n; i++) {
    memset(pre, 0, sizeof(pre));
    memset(mod, 0, sizeof(mod));
    for (int j = 1; j < i; j++) {
      pre[a[j]] = max(pre[a[j]], dp[i][j]);
      mod[a[j] % 7] = max(mod[a[j] % 7], dp[i][j]);
    }
    for (int j = i + 1; j <= n; j++) {
      dp[i][j] = max(pre[a[j] + 1], pre[a[j] - 1]) + 1;
      dp[i][j] = max(dp[i][j], mod[a[j] % 7] + 1);
      dp[i][j] = max(dp[i][j], dp[i][0] + 1);
      dp[j][i] = dp[i][j];
      pre[a[j]] = max(pre[a[j]], dp[i][j]);
      mod[a[j] % 7] = max(mod[a[j] % 7], dp[i][j]);
      ans = max(ans, dp[i][j]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
