#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << '{';
  for (int i = (0), __i = (((int)(vec).size())); i < __i; i++) {
    os << vec[i];
    if (i + 1 != ((int)(vec).size())) os << ',';
  }
  os << '}';
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &par) {
  os << '(' << par.first << ',' << par.second << ')';
  return os;
}
int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
template <class T>
T sqr(T x) {
  return x * x;
}
const int INF = 1000000000;
struct rib {
  int b, u, c, f;
  size_t back;
};
vector<vector<rib>> g(4 * 3030);
void add_rib(int a, int b, int u, int c) {
  rib r1 = {b, u, c, 0, g[b].size()};
  rib r2 = {a, 0, -c, 0, g[a].size()};
  g[a].push_back(r1);
  g[b].push_back(r2);
}
int a[3030];
int lastnum[101010];
int lastmod[7];
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = (0), __i = (n); i < __i; i++) cin >> a[i + 1];
  int N = 4 * n + 2;
  for (int i = (0), __i = (101010); i < __i; i++) lastnum[i] = -1;
  for (int i = (0), __i = (7); i < __i; i++) lastmod[i] = -1;
  for (int i = (0), __i = (n); i < __i; i++) {
    add_rib(0, 4 * i + 3, 1, 0);
    add_rib(4 * i + 1, 4 * i + 3, 1, 0);
    add_rib(4 * i + 2, 4 * i + 3, 1, 0);
    add_rib(4 * i + 3, 4 * i + 4, 1, -1);
    add_rib(4 * i + 4, N - 1, 1, 0);
    if (lastnum[a[n - i] - 1] != -1)
      add_rib(4 * (n - i - 1) + 4, 4 * (lastnum[a[n - i] - 1] - 1) + 1, 1, 0);
    if (lastnum[a[n - i] + 1] != -1)
      add_rib(4 * (n - i - 1) + 4, 4 * (lastnum[a[n - i] + 1] - 1) + 1, 1, 0);
    if (lastmod[a[n - i] % 7] != -1)
      add_rib(4 * (n - i - 1) + 4, 4 * (lastmod[a[n - i] % 7] - 1) + 2, 1, 0);
    if (lastnum[a[n - i]] != -1)
      add_rib(4 * (n - i - 1) + 1, 4 * (lastnum[a[n - i]] - 1) + 1, 4, 0);
    if (lastmod[a[n - i] % 7] != -1)
      add_rib(4 * (n - i - 1) + 2, 4 * (lastmod[a[n - i] % 7] - 1) + 2, 4, 0);
    lastnum[a[n - i]] = n - i;
    lastmod[a[n - i] % 7] = n - i;
  }
  int s = 0, t = N - 1;
  int flow = 0, cost = 0;
  while (flow < 4) {
    vector<int> d(N, INF);
    vector<int> p(N);
    vector<size_t> p_rib(N);
    d[s] = 0;
    while (true) {
      bool any = false;
      for (int i = (0), __i = (N); i < __i; i++) {
        if (d[i] < INF)
          for (int j = (0), __j = (((int)(g[i]).size())); j < __j; j++)
            if (g[i][j].f < g[i][j].u && d[g[i][j].b] > d[i] + g[i][j].c) {
              d[g[i][j].b] = d[i] + g[i][j].c;
              p[g[i][j].b] = i;
              p_rib[g[i][j].b] = j;
              any = true;
            }
      }
      if (!any) break;
    }
    if (d[t] == INF) break;
    int addflow = 4 - flow;
    for (int v = t; v != s; v = p[v]) {
      int pv = p[v];
      size_t pr = p_rib[v];
      addflow = min(addflow, g[pv][pr].u - g[pv][pr].f);
    }
    for (int v = t; v != s; v = p[v]) {
      int pv = p[v];
      size_t pr = p_rib[v], r = g[pv][pr].back;
      g[pv][pr].f += addflow;
      g[v][r].f -= addflow;
      cost += g[pv][pr].c * addflow;
    }
    flow += addflow;
  }
  cout << -cost << endl;
  return 0;
}
