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