#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 20;
const int oo = 1e9 + 20;
const int MOD = 1e9 + 7;
template <class type>
struct mcmf {
  struct edge {
    int u, v, cap, flow;
    type cost;
  };
  int n;
  vector<edge> ed;
  vector<vector<int>> g;
  vector<int> p;
  vector<type> d, phi;
  mcmf(int n) : n(n), g(n), p(n), d(n), phi(n) {}
  void add_edge(int u, int v, int cap, type cost) {
    g[u].push_back(ed.size());
    ed.push_back({u, v, cap, 0, cost});
    g[v].push_back(ed.size());
    ed.push_back({v, u, 0, 0, -cost});
  }
  bool dijkstra(int s, int t) {
    fill(d.begin(), d.end(), oo);
    fill(p.begin(), p.end(), -1);
    set<pair<type, int>> q;
    d[s] = 0;
    for (q.insert({d[s], s}); q.size();) {
      int u = (*q.begin()).second;
      q.erase(q.begin());
      for (auto v : g[u]) {
        auto &e = ed[v];
        type nd = d[e.u] + e.cost + phi[e.u] - phi[e.v];
        if (0 < (e.cap - e.flow) && nd < d[e.v]) {
          q.erase({d[e.v], e.v});
          d[e.v] = nd;
          p[e.v] = v;
          q.insert({d[e.v], e.v});
        }
      }
    }
    for (int i = 0; i < n; i++) phi[i] = min(oo, phi[i] + d[i]);
    return d[t] != oo;
  }
  pair<int, type> max_flow(int s, int t) {
    type mc = 0;
    int mf = 0;
    fill(phi.begin(), phi.end(), 0);
    while (dijkstra(s, t)) {
      int flow = oo;
      for (int v = p[t]; v != -1; v = p[ed[v].u])
        flow = min(flow, ed[v].cap - ed[v].flow);
      for (int v = p[t]; v != -1; v = p[ed[v].u]) {
        edge &e1 = ed[v];
        edge &e2 = ed[v ^ 1];
        mc += e1.cost * flow;
        e1.flow += flow;
        e2.flow -= flow;
      }
      mf += flow;
    }
    return {mf, mc};
  }
};
int a[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  while (cin >> n) {
    int source = 2 * n, sink = source + 1, inter = sink + 1;
    mcmf<int> net(inter + 1);
    net.add_edge(source, inter, 2, 0);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      net.add_edge(i, i + n, 1, -1);
      net.add_edge(inter, i, 1, 0);
      net.add_edge(i + n, sink, 1, 0);
    }
    for (int i = 0; i < n; i++) {
      int may = 0, same = 0, men = 0;
      for (int j = i + 1; j < n; j++) {
        if (a[i] % 7 == a[j] % 7 && same < 20)
          net.add_edge(i + n, j, 1, 0), same++;
        if (a[i] + 1 == a[j] && may < 20) net.add_edge(i + n, j, 1, 0), may++;
        if (a[i] - 1 == a[j] && men < 20) net.add_edge(i + n, j, 1, 0), men++;
      }
    }
    cout << -net.max_flow(source, sink).second << '\n';
  }
  return 0;
}
