#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    ;
  for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';)
    x = (x << 3) + (x << 1) + c - '0';
  return x;
}
struct edge {
  int nx, t, w, c;
} e[50000 * 2 + 5];
int a[3000 + 5], h[6000 + 5], en = 1, d[6000 + 5], q[6000 + 5], qn,
                              inq[6000 + 5], r[6000 + 5];
vector<int> v[100000 + 5], vv[7];
inline void ins(int x, int y, int w, int c) {
  e[++en] = (edge){h[x], y, w, c};
  h[x] = en;
  e[++en] = (edge){h[y], x, 0, -c};
  h[y] = en;
}
inline int next(int x) { return x < 6000 + 4 ? x + 1 : 0; }
bool spfa() {
  int i, j;
  memset(d, 200, sizeof(d));
  for (d[q[qn = 1, i = 0] = 6000 + 1] = 0; i != qn; inq[q[i]] = 0, i = next(i))
    for (j = h[q[i]]; j; j = e[j].nx)
      if (e[j].w && d[q[i]] + e[j].c > d[e[j].t]) {
        d[e[j].t] = d[q[i]] + e[j].c;
        r[e[j].t] = j;
        if (!inq[e[j].t]) inq[q[qn] = e[j].t] = 1, qn = next(qn);
      }
  return d[6000 + 2] > d[0];
}
int main() {
  int n = read(), i, j, ans = 0, mn;
  for (i = 1; i <= n; ++i)
    v[a[i] = read()].push_back(i), vv[a[i] % 7].push_back(i);
  ins(6000 + 1, 6000 + 2 + 1, 4, 0);
  for (i = 1; i <= n; ++i) {
    ins(6000 + 2 + 1, i, 1, 0);
    ins(i, i + n, 1, 1);
    ins(i + n, 6000 + 2, 1, 0);
    vector<int>::iterator it;
    it = upper_bound(v[a[i] - 1].begin(), v[a[i] - 1].end(), i);
    for (j = 0; j < 4; ++j)
      if (it != v[a[i] - 1].end())
        ins(i + n, *it++, 1, 0);
      else
        break;
    it = upper_bound(v[a[i] + 1].begin(), v[a[i] + 1].end(), i);
    for (j = 0; j < 4; ++j)
      if (it != v[a[i] + 1].end())
        ins(i + n, *it++, 1, 0);
      else
        break;
    it = upper_bound(vv[a[i] % 7].begin(), vv[a[i] % 7].end(), i);
    for (j = 0; j < 4; ++j)
      if (it != vv[a[i] % 7].end())
        ins(i + n, *it++, 1, 0);
      else
        break;
  }
  while (spfa()) {
    for (mn = 0x7FFFFFFF, i = 6000 + 2; i != 6000 + 1; i = e[r[i] ^ 1].t)
      mn = min(mn, e[r[i]].w);
    for (i = 6000 + 2; i != 6000 + 1; i = e[r[i] ^ 1].t)
      e[r[i]].w -= mn, e[r[i] ^ 1].w += mn;
    ans += mn * d[6000 + 2];
  }
  printf("%d", ans);
}
