#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int N = 6e3 + 100;
struct edge {
  int f, c, to, r;
};
vector<edge> G[N];
int S, T, prv[N], prp[N], cur[N], vst[N];
int d[N];
bool fst = false;
bool SPFA(int S) {
  if (fst) {
    d[S] = 0;
    for (int i = 1; i <= T; ++i) {
      d[i] = INF;
      int cnt = 0;
      for (edge e : G[i]) {
        if (!e.f) {
          if (d[e.to] - e.c < d[i]) {
            d[i] = d[e.to] - e.c;
            prv[i] = e.to;
            prp[i] = cnt;
          }
        }
        cnt++;
      }
    }
    fst = 0;
    return d[T] != INF;
  } else {
    fill(d, d + 1 + T, INF);
    priority_queue<pair<int, int>> pq;
    pq.push({0, S});
    d[S] = 0;
    while (1) {
      while (!pq.empty() && -pq.top().first != d[pq.top().second]) pq.pop();
      if (pq.empty()) break;
      int v = pq.top().second;
      pq.pop();
      int cnt = 0;
      for (edge e : G[v]) {
        if (e.f && d[e.to] > d[v] + e.c) {
          d[e.to] = d[v] + e.c;
          prv[e.to] = v;
          prp[e.to] = cnt;
          pq.push({-d[e.to], e.to});
        }
        cnt++;
      }
    }
    return d[T] != INF;
  }
}
int aug(int v, int flow) {
  if (v == T) return flow;
  vst[v] = 1;
  int flow1 = flow;
  for (int &i = cur[v]; i < (int)G[v].size(); ++i) {
    edge &e = G[v][i];
    if (e.f && d[v] + e.c == d[e.to] && !vst[e.to]) {
      int flow1 = aug(e.to, min(flow, e.f));
      flow -= flow1;
      e.f -= flow1;
      G[e.to][e.r].f += flow1;
    }
    if (flow == 0) {
      vst[v] = 0;
      return flow1 - flow;
    }
  }
  return flow1 - flow;
}
int mcmf() {
  int ans = 0, sT = 0;
  while (SPFA(S)) {
    sT += d[T];
    for (int i = 0; i <= T; ++i) cur[i] = 0, vst[i] = 0;
    ans += sT * aug(S, INF);
    for (int i = 0; i <= T; ++i) {
      for (auto &e : G[i]) {
        e.c += d[i] - d[e.to];
      }
    }
  }
  return ans;
}
void ae(int u, int v, int f, int c) {
  G[u].push_back((edge){f, c, v, (int)G[v].size()});
  G[v].push_back((edge){0, -c, u, (int)G[u].size() - 1});
}
int a[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  S = 0, T = 2 * n + 2;
  int SS = 1;
  ae(S, SS, 4, 0);
  for (int i = 1; i <= n; ++i) ae(2 * i, 2 * i + 1, 1, -1);
  for (int i = 1; i <= n; ++i) {
    ae(SS, 2 * i, 1, 0);
    ae(2 * i + 1, T, 1, 0);
    int cnt = 0;
    for (int j = i + 1; j <= n; ++j)
      if (a[j] % 7 == a[i] % 7) {
        ae(2 * i + 1, 2 * j, 1, 0);
        if (cnt++ == 4) break;
      }
    cnt = 0;
    for (int j = i + 1; j <= n; ++j)
      if (a[j] + 1 == a[i]) {
        ae(2 * i + 1, 2 * j, 1, 0);
        if (cnt++ == 4) break;
      }
    cnt = 0;
    for (int j = i + 1; j <= n; ++j)
      if (a[j] - 1 == a[i]) {
        ae(2 * i + 1, 2 * j, 1, 0);
        if (cnt++ == 4) break;
      }
  }
  int ans = mcmf();
  printf("%d\n", -ans);
}
