#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T, class X>
inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}
inline long long getnum() {
  char c = getchar();
  long long num, sign = 1;
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') sign = -1;
  for (num = 0; c >= '0' && c <= '9';) {
    c -= '0';
    num = num * 10 + c;
    c = getchar();
  }
  return num * sign;
}
inline long long power(long long a, long long b) {
  long long multiply = 1;
  for (int i = (0); i < (b); i++) {
    multiply *= a;
  }
  return multiply;
}
namespace mcmf {
const int MAX = 1000010;
const int INF = 1 << 25;
int cap[MAX], flow[MAX], cost[MAX], dis[MAX];
int n, m, s, t, Q[10000010], adj[MAX], link[MAX], last[MAX], from[MAX],
    visited[MAX];
void init(int nodes, int source, int sink) {
  m = 0, n = nodes, s = source, t = sink;
  for (int i = 0; i <= n; i++) last[i] = -1;
}
void addEdge(int u, int v, int c, int w) {
  adj[m] = v, cap[m] = c, flow[m] = 0, cost[m] = +w, link[m] = last[u],
  last[u] = m++;
  adj[m] = u, cap[m] = 0, flow[m] = 0, cost[m] = -w, link[m] = last[v],
  last[v] = m++;
}
bool spfa() {
  int i, j, x, f = 0, l = 0;
  for (i = 0; i <= n; i++) visited[i] = 0, dis[i] = INF;
  dis[s] = 0, Q[l++] = s;
  while (f < l) {
    i = Q[f++];
    for (j = last[i]; j != -1; j = link[j]) {
      if (flow[j] < cap[j]) {
        x = adj[j];
        if (dis[x] > dis[i] + cost[j]) {
          dis[x] = dis[i] + cost[j], from[x] = j;
          if (!visited[x]) {
            visited[x] = 1;
            if (f && rand() & 7)
              Q[--f] = x;
            else
              Q[l++] = x;
          }
        }
      }
    }
    visited[i] = 0;
  }
  return (dis[t] != INF);
}
pair<int, int> solve() {
  int i, j;
  int mincost = 0, maxflow = 0;
  while (spfa()) {
    int aug = INF;
    for (i = t, j = from[i]; i != s; i = adj[j ^ 1], j = from[i]) {
      aug = min(aug, cap[j] - flow[j]);
    }
    for (i = t, j = from[i]; i != s; i = adj[j ^ 1], j = from[i]) {
      flow[j] += aug, flow[j ^ 1] -= aug;
    }
    maxflow += aug, mincost += aug * dis[t];
  }
  return make_pair(mincost, maxflow);
}
}  // namespace mcmf
int n, a[5005];
int cnt[100005];
int main() {
  int test, cases = 1;
  scanf("%d", &n);
  for (int i = (1); i < (n + 1); i++) scanf("%d", &a[i]);
  int src = 0, mid = 2 * n + 2, sink = 2 * n + 4;
  mcmf::init(2 * n + 5, src, sink);
  mcmf::addEdge(src, mid, 2, 0);
  for (int i = (1); i < (n + 1); i++) mcmf::addEdge(i, i + n, 1, -1);
  for (int i = (1); i < (n + 1); i++) mcmf::addEdge(mid, i, 1, 0);
  for (int i = (1); i < (n + 1); i++) mcmf::addEdge(i + n, sink, 1, 0);
  for (int i = (1); i < (n + 1); i++) {
    for (int j = (1); j < (n + 1); j++) cnt[a[j]] = 0;
    for (int j = (i + 1); j < (n + 1); j++) {
      cnt[a[j]]++;
      if (abs(a[i] - a[j]) == 1 && cnt[a[j]] <= 5)
        mcmf::addEdge(i + n, j, 1, 0);
    }
  }
  for (int i = (1); i < (n + 1); i++) a[i] %= 7;
  for (int i = (1); i < (n + 1); i++) {
    for (int j = (1); j < (n + 1); j++) cnt[a[j]] = 0;
    for (int j = (i + 1); j < (n + 1); j++) {
      cnt[a[j]]++;
      if (a[i] == a[j] && cnt[a[j]] <= 5) mcmf::addEdge(i + n, j, 1, 0);
    }
  }
  pair<int, int> out = mcmf::solve();
  cout << -out.first << "\n";
  return 0;
}
