#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, next, from, f, c;
} e[4005 * 14];
int head[8010], dis[8010], vis[8010], q[8010], a[4005];
int from[8010], S, SS, T, tot = 1, ans;
void addE(int x, int y, int f, int c) {
  e[++tot] = (edge){y, head[x], x, f, c};
  head[x] = tot;
}
void add(int x, int y, int f, int c) {
  addE(x, y, f, c);
  addE(y, x, 0, -c);
}
bool bfs() {
  memset(dis, 63, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  int h = 0, t = 1;
  q[1] = S;
  dis[S] = 0;
  while (h != t) {
    if ((++h) == 8010) h = 0;
    int x = q[h];
    vis[x] = 0;
    for (int i = head[x]; i; i = e[i].next)
      if (dis[e[i].to] > dis[x] + e[i].c && e[i].f) {
        dis[e[i].to] = dis[x] + e[i].c;
        from[e[i].to] = i;
        if (!vis[e[i].to]) {
          vis[e[i].to] = 1;
          if ((++t) == 8010) t = 0;
          q[t] = e[i].to;
        }
      }
  }
  return dis[T] < 1e8;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  S = 0;
  SS = n * 2 + 1;
  T = n * 2 + 2;
  add(S, SS, 4, 0);
  for (int i = 1; i <= n; i++) {
    add(SS, i, 1, 0);
    add(n + i, T, 1, 0);
    add(i, n + i, 1, -1);
    add(i, n + i, 1e9, 0);
  }
  for (int i = 2; i <= n; i++) {
    for (int j = i - 1; j; j--)
      if (a[j] % 7 == a[i] % 7) {
        add(n + j, i, 1, 0);
        break;
      }
    for (int j = i - 1; j; j--)
      if (a[j] == a[i] + 1) {
        add(n + j, i, 1, 0);
        break;
      }
    for (int j = i - 1; j; j--)
      if (a[j] == a[i] - 1) {
        add(n + j, i, 1, 0);
        break;
      }
  }
  while (bfs()) {
    ans -= dis[T];
    for (int i = from[T]; i; i = from[e[i].from]) e[i].f--, e[i ^ 1].f++;
  }
  printf("%d", ans);
}
