#include <bits/stdc++.h>
using namespace std;
int MOD = 1e9 + 7;
int INF = 0x3f3f3f3f;
struct edge {
  int from, to, cap, weight;
  edge *rev;
  edge(int u, int v, int c, int w)
      : from(u), to(v), cap(c), weight(w), rev(NULL) {}
};
const int V = 12005;
int n, s, t;
edge *p[V];
int dist[V], vis[V], pote[V];
vector<edge *> g[V];
void addedge(int u, int v, int c, int w) {
  edge *in = new edge(u, v, c, w);
  edge *out = new edge(v, u, 0, -w);
  in->rev = out;
  out->rev = in;
  g[u].push_back(in);
  g[v].push_back(out);
}
int mincostflow() {
  int mxf = 0, mnc = 0;
  for (int i = 0; i < n; i++) vis[i] = 0;
  for (int i = 0; i < n; i++) pote[i] = INF;
  queue<int> q;
  q.push(s);
  pote[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (edge *e : g[u]) {
      int v = e->to;
      if (e->cap > 0 && pote[u] + e->weight < pote[v]) {
        pote[v] = pote[u] + e->weight;
        if (!vis[v]) q.push(v), vis[v] = 1;
      }
    }
  }
  for (;;) {
    for (int i = 0; i < n; i++) dist[i] = INF, p[i] = NULL;
    priority_queue<pair<int, int> > pq;
    pq.push({dist[s] = 0, s});
    while (!pq.empty()) {
      int u = pq.top().second;
      pq.pop();
      for (edge *e : g[u])
        if (e->cap > 0) {
          int v = e->to, t = dist[u] + e->weight + pote[u] - pote[v];
          if (t < dist[v]) pq.push({-(dist[v] = t), v}), p[v] = e;
        }
    }
    for (int i = 0; i < n; i++)
      if (pote[i] < INF && dist[i] < INF) pote[i] += dist[i];
    if (dist[t] == INF) break;
    int fo = INF;
    for (int u = t; p[u] != NULL; u = p[u]->from) fo = min(fo, p[u]->cap);
    for (int u = t; p[u] != NULL; u = p[u]->from) {
      p[u]->cap -= fo, p[u]->rev->cap += fo;
      mnc += fo * p[u]->weight;
    }
    mxf += fo;
  }
  return mnc;
}
int N, a[3005];
vector<int> idx[100005], idx2[7];
int solve() {
  cin >> N;
  for (int i = 0; i < N; i++) cin >> a[i];
  for (int i = 0; i < N; i++)
    idx[a[i]].push_back(i), idx2[a[i] % 7].push_back(i);
  n = N * 4 + 3;
  s = N * 4, t = N * 4 + 2;
  int midman = N * 4 + 1;
  for (int i = 0; i < N; i++) addedge(i, N + i, 1, -1);
  addedge(s, midman, 4, 0);
  for (int i = 0; i < N; i++) addedge(midman, i, 1, 0);
  for (int i = 0; i < N; i++) addedge(N + i, t, 1, 0);
  for (int i = 1; i <= 100000; i++)
    for (int j = 0; j + 1 < idx[i].size(); j++)
      addedge(N * 2 + idx[i][j], N * 2 + idx[i][j + 1], INF, 0);
  for (int i = 0; i < N; i++) addedge(N * 2 + i, i, 1, 0);
  for (int i = 0; i < 7; i++)
    for (int j = 0; j + 1 < idx2[i].size(); j++)
      addedge(N * 3 + idx2[i][j], N * 3 + idx2[i][j + 1], INF, 0);
  for (int i = 0; i < N; i++) addedge(N * 3 + i, i, 1, 0);
  for (int i = 0; i < N; i++) {
    int nout = N + i;
    auto in1 = upper_bound(idx[a[i] + 1].begin(), idx[a[i] + 1].end(), i);
    if (in1 != idx[a[i] + 1].end()) addedge(nout, N * 2 + *in1, 1, 0);
    auto in2 = upper_bound(idx[a[i] - 1].begin(), idx[a[i] - 1].end(), i);
    if (in2 != idx[a[i] - 1].end()) addedge(nout, N * 2 + *in2, 1, 0);
    auto in3 = upper_bound(idx2[a[i] % 7].begin(), idx2[a[i] % 7].end(), i);
    if (in3 != idx2[a[i] % 7].end()) addedge(nout, N * 3 + *in3, 1, 0);
  }
  cout << -mincostflow() << endl;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  solve();
  return 0;
}
