#include <bits/stdc++.h>
using namespace std;
int dp[5100][5100], f[100100], g[7], a[5100];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    memset(f, 0, sizeof f);
    memset(g, 0, sizeof g);
    for (int j = 0; j <= n; j++) {
      if (j == i) continue;
      if (i < j) {
        dp[i][j] = max(dp[i][j], f[a[j] + 1] + 1);
        dp[i][j] = max(dp[i][j], f[a[j] - 1] + 1);
        dp[i][j] = max(dp[i][j], g[a[j] % 7] + 1);
        dp[i][j] = max(dp[i][j], dp[i][0] + 1);
        ans = max(ans, dp[i][j]);
        f[a[j]] = max(f[a[j]], dp[i][j]);
        g[a[j] % 7] = max(g[a[j]] % 7, dp[i][j]);
      } else {
        dp[i][j] = dp[j][i];
        ans = max(ans, dp[i][j]);
        f[a[j]] = max(f[a[j]], dp[i][j]);
        g[a[j] % 7] = max(g[a[j] % 7], dp[i][j]);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
