#include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const int MOD2 = (int)1e8 + 7;   const int INF = (int)1e9;   const long long LINF = (long long)1e18;   const long double PI = acos((long double)-1);   const long double EPS = 1e-9;   inline long long gcd(long long a, long long b) {     long long r;     while (b) {       r = a % b;       a = b;       b = r;     }     return a;   }   inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   inline long long fpow(long long n, long long k, int p = MOD) {     long long r = 1;     for (; k; k >>= 1) {       if (k & 1) r = r * n % p;       n = n * n % p;     }     return r;   }   template <class T>   inline int chkmin(T& a, const T& val) {     return val < a ? a = val, 1 : 0;   }   template <class T>   inline int chkmax(T& a, const T& val) {     return a < val ? a = val, 1 : 0;   }   inline long long isqrt(long long k) {     long long r = sqrt(k) + 1;     while (r * r > k) r--;     return r;   }   inline long long icbrt(long long k) {     long long r = cbrt(k) + 1;     while (r * r * r > k) r--;     return r;   }   inline void addmod(int& a, int val, int p = MOD) {     if ((a = (a + val)) >= p) a -= p;   }   inline void submod(int& a, int val, int p = MOD) {     if ((a = (a - val)) < 0) a += p;   }   inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }   inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }   inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }   inline int sign(long double x, long double y) { return sign(x - y); }   template <class TF, class TC, TF Foo, TC Coo>   struct MinCostMaximumFlow {     static const int MAXV = 1e5 + 5;     static const int MAXE = 1e6 + 5;     int n, s, t, E;     int adj[MAXE], nxt[MAXE], lst[MAXV], frm[MAXV], vis[MAXV];     TF cap[MAXE], flw[MAXE], totalFlow;     TC cst[MAXE], dst[MAXV], totalCost;     void init(int n, int s, int t) {       this->n = n, this->s = s, this->t = t;       fill_n(lst, n, -1), E = 0;     }     void add(int u, int v, TF ca, TC co) {       adj[E] = v, cap[E] = ca, flw[E] = 0, cst[E] = +co, nxt[E] = lst[u],       lst[u] = E++;       adj[E] = u, cap[E] = 0, flw[E] = 0, cst[E] = -co, nxt[E] = lst[v],       lst[v] = E++;     }     int spfa() {       fill_n(dst, n, Coo), dst[s] = 0;       queue<int> que;       que.push(s);       while (que.size()) {         int u = que.front();         que.pop();         for (int e = lst[u]; e != -1; e = nxt[e])           if (flw[e] < cap[e]) {             int v = adj[e];             if (dst[v] > dst[u] + cst[e]) {               dst[v] = dst[u] + cst[e];               frm[v] = e;               if (!vis[v]) {                 vis[v] = 1;                 que.push(v);               }             }           }         vis[u] = 0;       }       return dst[t] < Coo;     }     TC mincost() {       totalCost = 0, totalFlow = 0;       while (1) {         if (!spfa()) break;         TF mn = Foo;         for (int v = t, e = frm[v]; v != s; v = adj[e ^ 1], e = frm[v])           mn = min(mn, cap[e] - flw[e]);         for (int v = t, e = frm[v]; v != s; v = adj[e ^ 1], e = frm[v]) {           flw[e] += mn;           flw[e ^ 1] -= mn;         }         totalFlow += mn;         totalCost += mn * dst[t];       }       return totalCost;     }   };   MinCostMaximumFlow<int, int, (int)1e9, (int)1e9> mcmf;   const int maxn = 5e3 + 5;   int n;   int a[maxn];   int num[100005];   void solve() {     cin >> n;     for (int i = (0); i < (n); i++) cin >> a[i];     int tot = n + n, s = tot++, t = tot++, ss = tot++;     mcmf.init(tot, ss, t);     mcmf.add(ss, s, 2, 0);     for (int i = (0); i < (n); i++) {       mcmf.add(s, i, 1, 0);       mcmf.add(i + n, t, 1, 0);       mcmf.add(i, i + n, 1, -1);       static int* his[maxn];       int nhis = 0;       for (int j = (i + 1); j < (n); j++)         if (abs(a[i] - a[j]) == 1) {           his[nhis++] = &num[a[j]];           if (num[a[j]]++ <= 4) {             mcmf.add(i + n, j, 1, 0);           }         }       for (int j = (0); j < (nhis); j++) (*his[j])--;     }     vector<int> lst[7];     for (int i = (n)-1; i >= (0); i--) {       int r = a[i] % 7;       for (int j = (0); j < (int((lst[r]).size())); j++) {         mcmf.add(i + n, lst[r][j], 1, 0);       }       while (int((lst[r]).size()) > 4) lst[r].erase(lst[r].begin());       lst[r].push_back(i);     }     cout << -mcmf.mincost() <<   n ;   }   int main() {     int JUDGE_ONLINE = 1;     if (fopen( in.txt ,  r )) {       JUDGE_ONLINE = 0;       assert(freopen( in.txt ,  r , stdin));     } else {       ios_base::sync_with_stdio(0), cin.tie(0);     }     solve();     if (!JUDGE_ONLINE) {     }     return 0;   }