#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, z, g[20000 + 5], d[20000 + 5], pre[20000 + 5], S, T, now,
    ans, a[20000 + 5];
struct yyy {
  int to, w, g, z;
} tmp;
struct ljb {
  int head, h[20000 + 5];
  yyy f[20000 + 5 << 5];
  inline void add(int x, int y, int z, int g) {
    f[head] = (yyy){y, z, g, h[x]};
    h[x] = head++;
  }
} s;
queue<int> q;
inline int bfs() {
  memset(d, -0x3f, sizeof(d));
  memset(g, 0, sizeof(g));
  q.push(S);
  d[S] = 0;
  g[S] = 1e9;
  while (!q.empty()) {
    now = q.front();
    q.pop();
    for (int i = s.h[now]; ~i; i = tmp.z) {
      tmp = s.f[i];
      if (!tmp.g || d[tmp.to] >= d[now] + tmp.w) continue;
      d[tmp.to] = d[now] + tmp.w;
      g[tmp.to] = ((g[now]) < (tmp.g) ? (g[now]) : (tmp.g));
      q.push(tmp.to);
      pre[tmp.to] = i;
    }
  }
  return d[T] > 0;
}
inline void get(int x, int y, int z, int g) {
  s.add(x, y, z, g);
  s.add(y, x, -z, 0);
}
int main() {
  memset(s.h, -1, sizeof(s.h));
  register int i, j;
  scanf("%d", &n);
  T = 4 * n + 1;
  S = 4 * n + 2;
  get(S, 0, 0, 2);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    get(0, i, 0, 2e9), get(i + n, T, 0, 2e9), get(i, i + n, 1, 1),
        get(i + 2 * n, i, 0, 2e9), get(i + 3 * n, i, 0, 2e9);
    for (j = i + 1; j <= n; j++)
      if (a[i] % 7 == a[j] % 7) {
        get(i + n, j + 2 * n, 0, 2e9), get(i + 2 * n, j + 2 * n, 0, 2e9);
        break;
      }
    for (j = i + 1; j <= n; j++)
      if (a[j] == a[i] - 1) {
        get(i + n, j + 3 * n, 0, 2e9);
        break;
      }
    for (j = i + 1; j <= n; j++)
      if (a[j] == a[i] + 1) {
        get(i + n, j + 3 * n, 0, 2e9);
        break;
      }
    for (j = i + 1; j <= n; j++)
      if (a[i] == a[j]) {
        get(i + 3 * n, j + 3 * n, 0, 2e9);
        break;
      }
  }
  while (bfs()) {
    ans += g[T] * d[T];
    now = T;
    while (now ^ S)
      s.f[pre[now]].g -= g[T], s.f[pre[now] ^ 1].g += g[T],
          now = s.f[pre[now] ^ 1].to;
  }
  printf("%d\n", ans);
}
