#include <bits/stdc++.h>
using namespace std;
constexpr int inf32 = 0x3f3f3f3f;
constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f;
constexpr int F = 4;
struct flows {
  struct edge {
    int v, w, c;
    edge *first, *r;
    edge(int v, int w, int c, edge *first)
        : v(v), w(w), c(c), first(first), r(nullptr) {}
  };
  int n, s, t;
  vector<int> d, vis;
  vector<edge *> adj, p;
  queue<int> q;
  flows(int n, int s, int t) : n(n), s(s), t(t), adj(n), d(n), p(n), vis(n) {}
  void adde(int u, int v, int w, int c = 0) {
    adj[u] = new edge(v, w, +c, adj[u]);
    adj[v] = new edge(u, 0, -c, adj[v]);
    (adj[u]->r = adj[v])->r = adj[u];
  }
  bool expath() {
    fill((d).begin(), (d).end(), -1);
    fill((p).begin(), (p).end(), nullptr);
    fill((vis).begin(), (vis).end(), false);
    q.push(s), d[s] = 0, vis[s] = true;
    while ((int)((q).size())) {
      int u = q.front();
      q.pop(), vis[u] = false;
      for (edge *e = adj[u]; e; e = e->first)
        if (e->w && d[e->v] < d[u] + e->c) {
          d[e->v] = d[u] + e->c, p[e->v] = e;
          if (!vis[e->v]) vis[e->v] = true, q.push(e->v);
        }
    }
    return ~d[t];
  }
  pair<int, int> calc(pair<int, int> res = {0, 0}) {
    while (expath()) {
      int f = inf32;
      for (int u = t; u != s; u = p[u]->r->v) f = min(f, p[u]->w);
      for (int u = t; u != s; u = p[u]->r->v) p[u]->w -= f, p[u]->r->w += f;
      res.first += f, res.second += d[t] * f;
      if (res.first == F) break;
    }
    return res;
  }
};
constexpr int N = 1e5 + 1;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  int s = n + n + 1, t = n + n + 2;
  flows G(t + 1, s, t);
  G.adde(s, n + n, F);
  vector<int> pm(7, -1), pd(N, -1);
  for (int i = (n)-1; i >= 0; --i) {
    G.adde(n + n, i, 1), G.adde(i + n, t, 1);
    G.adde(i, i + n, 1, 1);
    int &m = pm[a[i] % 7];
    if (~m) G.adde(i, m, F), G.adde(i + n, m, F);
    if (a[i] > 0 && ~pd[a[i] - 1]) G.adde(i + n, pd[a[i] - 1], F);
    if (a[i] + 1 < N && ~pd[a[i] + 1]) G.adde(i + n, pd[a[i] + 1], F);
    m = pd[a[i]] = i;
  }
  cout << G.calc().second << '\n';
  return 0;
}
