#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
const int M = 1000000007;
const double PI = atan(1) * 4;
const int oo = 1000000000;
int n, dp[5001][5001], v[5001], mxm[7], mxn[N];
int main() {
  cin >> n;
  for (int i = 1; i < n + 1; ++i) scanf("%d", &v[i]);
  int ans = 0;
  for (int j = 0; j <= n; ++j) {
    memset(mxn, 0, sizeof(mxn));
    memset(mxm, 0, sizeof(mxm));
    for (int i = 0; i <= n; ++i) {
      dp[i][j] = dp[j][i];
      int d = v[i];
      if (i > j) {
        dp[i][j] = 1 + mxm[d % 7];
        dp[i][j] = max(dp[i][j], 1 + dp[0][j]);
        dp[i][j] = max(dp[i][j], 1 + mxn[d - 1]);
        dp[i][j] = max(dp[i][j], 1 + mxn[d + 1]);
      }
      mxm[d % 7] = max(mxm[d % 7], dp[i][j]);
      mxn[d] = max(mxn[d], dp[i][j]);
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << endl;
}
