#include <bits/stdc++.h>
using namespace std;
int n;
int a[5001];
int dp[5001][5001];
int lmod[7];
int lnum[100011];
int main() {
  int ans = 0;
  cin >> n;
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n + 1; i++) {
    memset(lmod, 0, sizeof(lmod));
    for (int j = 1; j < n + 1; j++) {
      if (j > i)
        dp[i][j] = dp[j][i] = max(max(dp[i][0], lmod[a[j] % 7]),
                                  max(lnum[a[j] - 1], lnum[a[j] + 1])) +
                              1;
      lnum[a[j]] = max(lnum[a[j]], dp[i][j]);
      lmod[a[j] % 7] = max(lmod[a[j] % 7], dp[i][j]);
      ans = max(ans, dp[i][j]);
    }
    for (int j = 1; j < n + 1; j++) lnum[a[j]] = 0;
  }
  cout << ans << endl;
}
