#include <bits/stdc++.h>
using namespace std;
const int NN = 5010;
inline char get(void) {
  static char buf[100000], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);
    if (p1 == p2) return EOF;
  }
  return *p1++;
}
inline void read(int &x) {
  x = 0;
  static char c;
  for (; !(c >= '0' && c <= '9'); c = get())
    ;
  for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = get())
    ;
}
inline void Max(int &x, const int &y) {
  if (y > x) x = y;
}
int X[NN];
int N, a[NN], self[NN], pre[NN], nxt[NN];
int dx[NN][NN], dy[NN][NN], x7[NN][10], y7[NN][10], stx[NN], sty[NN], Ans;
int main(void) {
  read(N);
  for (int i = 1; i <= N; i++) {
    read(a[i]);
    X[++*X] = a[i];
  }
  sort(X + 1, X + *X + 1);
  X[ *X = (unique(X + 1, X + *X + 1) - X)] = 1 << 30;
  for (int i = 1; i <= N; i++) {
    self[i] = lower_bound(X + 1, X + *X + 1, a[i]) - X;
    pre[i] = a[i] - 1 == X[self[i] - 1] ? self[i] - 1 : -1;
    nxt[i] = a[i] + 1 == X[self[i] + 1] ? self[i] + 1 : -1;
  }
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= N; j++) {
      if (i == j) continue;
      int f = 0;
      if (i > j) {
        if (pre[i] != -1) Max(f, dy[j][pre[i]] + 1);
        if (nxt[i] != -1) Max(f, dy[j][nxt[i]] + 1);
        Max(f, y7[j][a[i] % 7] + 1);
        Max(f, sty[j] + 1);
      } else {
        if (pre[j] != -1) Max(f, dx[i][pre[j]] + 1);
        if (nxt[j] != -1) Max(f, dx[i][nxt[j]] + 1);
        Max(f, x7[i][a[j] % 7] + 1);
        Max(f, stx[i] + 1);
      }
      Max(Ans, f);
      if (j > 0) {
        Max(dx[i][self[j]], f);
        Max(x7[i][a[j] % 7], f);
      }
      if (i > 0) {
        Max(dy[j][self[i]], f);
        Max(y7[j][a[i] % 7], f);
      }
      if (i == 0) sty[j] = f;
      if (j == 0) stx[i] = f;
    }
  cout << Ans << endl;
  return 0;
}
