#include <bits/stdc++.h>
using namespace std;
int n, m, s, t, h[20005], tot = 1;
struct edge {
  int to, nxt, cap, val;
} e[50005 << 1];
void add(int x, int y, int z, int w) {
  e[++tot].nxt = h[x];
  h[x] = tot;
  e[tot].to = y;
  e[tot].val = w;
  e[tot].cap = z;
  e[++tot].nxt = h[y];
  h[y] = tot;
  e[tot].to = x;
  e[tot].val = -w;
  e[tot].cap = 0;
}
queue<int> q;
int v[20005], d[20005], pre[20005], cur[20005], flow, ans;
bool bfs() {
  memset(d, 0x3f, sizeof(d));
  memset(v, 0, sizeof(v));
  d[s] = 0;
  q.push(s);
  cur[s] = 1;
  cur[t] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    v[x] = 0;
    for (int i = h[x]; i; i = e[i].nxt)
      if (e[i].cap && d[x] + e[i].val < d[e[i].to]) {
        d[e[i].to] = d[x] + e[i].val;
        pre[e[i].to] = i;
        cur[e[i].to] = min(cur[x], e[i].cap);
        if (!v[e[i].to]) v[e[i].to] = 1, q.push(e[i].to);
      }
  }
  return cur[t] ? 1 : 0;
}
void updata() {
  flow += cur[t];
  ans += cur[t] * d[t];
  int x = t;
  while (x != s) {
    e[pre[x]].cap -= cur[t];
    e[pre[x] ^ 1].cap += cur[t];
    x = e[pre[x] ^ 1].to;
  }
}
int a[20005], c[10], u[50005 << 1];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  s = 4 * n + 2;
  t = s + 1;
  for (int i = 1; i <= n; i++)
    add(s, i + n + n, 2, 0), add(i + n * 3, t, 2, 0), add(i, i + n + n, 2, 0),
        add(i + n, i + n + n, 2, 0), add(i + n + n, i + n * 3, 1, -1);
  for (int i = n; i >= 1; i--) {
    if (u[a[i]]) add(i, u[a[i]], 2, 0);
    if (u[a[i] + 1]) add(i + n * 3, u[a[i] + 1], 2, 0);
    if (u[a[i] - 1]) add(i + n * 3, u[a[i] - 1], 2, 0);
    if (c[a[i] % 7])
      add(i + n, c[a[i] % 7] + n, 2, 0), add(i + n * 3, c[a[i] % 7] + n, 2, 0);
    c[a[i] % 7] = u[a[i]] = i;
  }
  while (flow < 2) bfs(), updata();
  printf("%d\n", -ans);
  return 0;
}
