#include <bits/stdc++.h>
using namespace std;
const int N = 6005;
const int M = 100000;
const int inf = 1e9;
struct Edge {
  int v, w, cost, next;
} g[M];
int head[N], st, ed, flow, cost, cnt;
void init() {
  memset(head, -1, sizeof(head));
  flow = cost = cnt = 0;
}
void addEdge(int u, int v, int w, int cost) {
  g[cnt].v = v, g[cnt].w = w, g[cnt].cost = cost, g[cnt].next = head[u],
  head[u] = cnt++;
  g[cnt].v = u, g[cnt].w = 0, g[cnt].cost = -cost, g[cnt].next = head[v],
  head[v] = cnt++;
}
bool spfaFlow(int n) {
  vector<int> dis(n + 1, inf);
  vector<bool> vis(n + 1, false);
  vector<int> path(n + 1, -1);
  dis[st] = 0;
  vis[st] = true;
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; ~i; i = g[i].next) {
      int v = g[i].v;
      if (g[i].w && dis[v] > dis[u] + g[i].cost) {
        dis[v] = dis[u] + g[i].cost;
        path[v] = i;
        if (!vis[v]) {
          vis[v] = true;
          q.push(v);
        }
      }
    }
  }
  int mxflow = inf;
  if (path[ed] != -1) {
    for (int u = ed; u != st; u = g[path[u] ^ 1].v) {
      mxflow = min(mxflow, g[path[u]].w);
    }
    flow += mxflow;
    for (int u = ed; u != st; u = g[path[u] ^ 1].v) {
      g[path[u]].w -= mxflow;
      g[path[u] ^ 1].w += mxflow;
      cost += mxflow * g[path[u]].cost;
    }
  }
  return path[ed] != -1;
}
int minCost(int n) {
  while (spfaFlow(n))
    ;
  return cost;
}
int main(int argc, char *argv[]) {
  cin.sync_with_stdio(false);
  init();
  int n;
  cin >> n;
  vector<int> a(n + 1);
  vector<deque<int>> b(100002), c(7);
  st = 2 * n + 1, ed = 2 * n + 3;
  int sed = 2 * n + 2;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i]].push_back(i);
    c[a[i] % 7].push_back(i);
    addEdge(st, i, 1, 0);
    addEdge(i, i + n, 1, -1);
    addEdge(i + n, sed, 1, 0);
  }
  for (int i = 1; i <= n; i++) {
    b[a[i]].pop_front();
    c[a[i] % 7].pop_front();
    int cnt = 4;
    for (auto x : b[a[i] - 1]) {
      addEdge(i + n, x, 1, 0);
      if (--cnt == 0) break;
    }
    cnt = 4;
    for (auto x : b[a[i] + 1]) {
      addEdge(i + n, x, 1, 0);
      if (--cnt == 0) break;
    }
    cnt = 4;
    for (auto x : c[a[i] % 7]) {
      addEdge(i + n, x, 1, 0);
      if (--cnt == 0) break;
    }
  }
  addEdge(sed, ed, 4, 0);
  cout << -minCost(2 * n + 3) << endl;
  return 0;
}
