#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[5005], f[5005][5005];
long long ans, p1[100005], p2[10];
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
    if (ch == -1) return 0;
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline long long max_(long long a, long long b, long long c, long long d) {
  return max(max(a, b), max(c, d));
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) a[i] = read();
  for (long long i = 0; i <= n; i++) {
    memset(p1, 0, sizeof(p1)), memset(p2, 0, sizeof(p2));
    for (long long j = 1; j <= i - 1; j++)
      p1[a[j]] = max(p1[a[j]], f[i][j]),
      p2[a[j] % 7] = max(p2[a[j] % 7], f[i][j]);
    for (long long j = i + 1; j <= n; j++) {
      f[i][j] = f[j][i] = max_(f[i][0] + 1, p1[a[j] + 1] + 1, p1[a[j] - 1] + 1,
                               p2[a[j] % 7] + 1);
      p1[a[j]] = max(p1[a[j]], f[i][j]),
      p2[a[j] % 7] = max(p2[a[j] % 7], f[i][j]), ans = max(ans, f[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}
