#include <bits/stdc++.h>
using namespace std;
struct node {
  int d, id;
} A[5100];
int f[5100][5100], a[5100], r[5100], n, mx, nex[5100][5100], mnex[5100][7], ans;
bool cmp(node x, node y) {
  if (x.d < y.d) return 1;
  return 0;
}
void upd(int &x, int y) { x = max(x, y); }
void lsh() {
  for (int i = 1; i <= n; i++) A[i].d = a[i], A[i].id = i;
  sort(A + 1, A + n + 1, cmp);
  a[A[1].id] = 1;
  int k = 1;
  r[1] = A[1].d;
  for (int i = 2; i <= n; i++) {
    if (A[i].d != A[i - 1].d) k++;
    a[A[i].id] = k;
    r[k] = A[i].d;
  }
  mx = k;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  lsh();
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 1; j <= mx; j++) nex[i][j] = nex[i + 1][j];
    nex[i][a[i + 1]] = i + 1;
    for (int j = 0; j < 7; j++) mnex[i][j] = mnex[i + 1][j];
    mnex[i][r[a[i + 1]] % 7] = i + 1;
  }
  for (int i = 1; i <= n; i++) f[i][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < i; j++) {
      int x = mnex[i][r[a[i]] % 7];
      if (x) upd(f[x][j], f[i][j] + 1);
      x = nex[i][a[i] + 1];
      if (x && r[a[i]] + 1 == r[a[i] + 1]) upd(f[x][j], f[i][j] + 1);
      x = nex[i][a[i] - 1];
      if (x && r[a[i]] - 1 == r[a[i] - 1]) upd(f[x][j], f[i][j] + 1);
      if (j) {
        if (i == 4 && j == 1) {
          int oo = 1;
        }
        x = mnex[i][r[a[j]] % 7];
        if (x) upd(f[x][i], f[i][j] + 1);
        x = nex[i][a[j] + 1];
        if (x && r[a[j]] + 1 == r[a[j] + 1]) upd(f[x][i], f[i][j] + 1);
        x = nex[i][a[j] - 1];
        if (x && r[a[j]] - 1 == r[a[j] - 1]) upd(f[x][i], f[i][j] + 1);
      } else
        for (int k = i + 1; k <= n; k++) upd(f[k][i], f[i][j] + 1);
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++) upd(ans, f[i][j]);
  printf("%d\n", ans);
  return 0;
}
