#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0;
  int ch = getchar(), f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void rt(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10)
    rt(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
const int maxn = 5005, S = 100005;
int n, m, a[maxn], id[S], dsc[maxn];
int g[maxn][maxn], h[maxn][7], ans;
void upd(int &x, int y) {
  if (y > x) x = y;
}
int main() {
  n = rd();
  for (int i = int(1); i <= (int)(n); i++) a[i] = dsc[i] = rd();
  sort(dsc + 1, dsc + n + 1);
  m = unique(dsc + 1, dsc + n + 1) - dsc - 1;
  for (int i = int(1); i <= (int)(m); i++) id[dsc[i]] = i;
  for (int i = int(1); i <= (int)(n); i++) {
    int x = id[a[i]];
    for (int j = int(0); j <= (int)(i - 1); j++) {
      int t = 0;
      if (x && dsc[x - 1] + 1 == a[i]) upd(t, g[j][x - 1] + 1);
      if (x < m && dsc[x + 1] - 1 == a[i]) upd(t, g[j][x + 1] + 1);
      upd(t, h[j][a[i] % 7] + 1);
      if (j)
        upd(h[i][a[j] % 7], t), upd(g[i][id[a[j]]], t);
      else {
        for (int k = int(0); k <= (int)(6); k++) upd(h[i][k], t);
        for (int k = int(1); k <= (int)(m); k++) upd(g[i][k], t);
      }
      upd(g[j][id[a[i]]], t), upd(h[j][a[i] % 7], t);
      upd(ans, t);
    }
  }
  rt(ans), putchar('\n');
}
