#include <bits/stdc++.h>
using namespace std;
int n, x, y, z, i, j, res;
int a[5010], g[5010];
short int t[5010][5010][3], f[5010][5010];
short int get(short int x, short int y) {
  if (x == n + 1) return -1;
  if (f[x][y] >= 0) return f[x][y];
  f[x][y] = 0;
  short int xx, yy;
  for (int i = 0; i <= 2; i++) {
    xx = get(t[x][x + 1][i], y) + 1;
    yy = get(t[y][x + 1][i], x) + 1;
    f[x][y] = max(f[x][y], max(xx, yy));
  }
  return f[x][y];
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) {
    g[i] = 1;
    for (j = 1; j < i; j++) {
      if (a[j] - a[i] == 1) g[i] = max(g[i], g[j] + 1);
      if (a[i] - a[j] == 1) g[i] = max(g[i], g[j] + 1);
      ;
      if (abs(a[i] - a[j]) % 7 == 0) g[i] = max(g[i], g[j] + 1);
      ;
    }
  }
  for (i = 1; i <= n; i++) {
    x = n + 1;
    y = n + 1;
    z = n + 1;
    t[i][n + 1][0] = t[i][n + 1][1] = t[i][n + 1][2] = n + 1;
    for (j = n; j >= i + 1; j--) {
      if (a[j] - a[i] == 1) x = j;
      if (a[i] - a[j] == 1) y = j;
      if (abs(a[i] - a[j]) % 7 == 0) z = j;
      t[i][j][0] = x;
      t[i][j][1] = y;
      t[i][j][2] = z;
    }
  }
  memset(f, -1, sizeof(f));
  for (i = 1; i < n; i++)
    for (j = i + 1; j <= n; j++) res = max(res, g[i] + 1 + get(j, i));
  printf("%d", res);
}
