#include <bits/stdc++.h>
using namespace std;
const int MN = 5010;
int N;
int A[MN], chk[100010];
int Xn;
vector<int> X;
map<int, int> dx;
int mx1[MN][MN], mx2[MN][7];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
    X.push_back(A[i]);
    chk[A[i]] = 1;
  }
  sort(X.begin(), X.end());
  X.resize(unique(X.begin(), X.end()) - X.begin());
  Xn = X.size();
  for (int i = 0; i < Xn; i++) dx[X[i]] = i;
  for (int i = 1; i <= N; i++) A[i] = dx[A[i]];
  int ans = 0;
  for (int i = N; i >= 0; i--) {
    for (int j = N; j > i; j--) {
      int t = 0;
      if (chk[X[A[j]] + 1]) t = max(t, mx1[i][A[j] + 1]);
      if (X[A[j]] && chk[X[A[j]] - 1]) t = max(t, mx1[i][A[j] - 1]);
      t = max(t, mx2[i][X[A[j]] % 7]);
      if (i) {
        if (chk[X[A[i]] + 1]) t = max(t, mx1[j][A[i] + 1]);
        if (X[A[i]] && chk[X[A[i]] - 1]) t = max(t, mx1[j][A[i] - 1]);
        t = max(t, mx2[j][X[A[i]] % 7]);
      } else {
        for (int k = 0; k < MN; k++) t = max(t, mx1[j][k]);
        for (int k = 0; k < 7; k++) t = max(t, mx2[j][k]);
      }
      t++;
      mx1[i][A[j]] = max(mx1[i][A[j]], t);
      mx2[i][X[A[j]] % 7] = max(mx2[i][X[A[j]] % 7], t);
      ans = max(ans, t);
    }
  }
  printf("%d", ans);
}
