#include <bits/stdc++.h>
using namespace std;
long long pwr(long long base, long long p, long long mod = (1000000007LL)) {
  long long ans = 1;
  while (p) {
    if (p & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int n;
int a[5005], maxmod[10], maxnum[100005], dp[5005][5005], ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int j = 0; j <= n; j++) {
    memset(maxmod, 0, sizeof(maxmod));
    memset(maxnum, 0, sizeof(maxnum));
    for (int i = 0; i < j; i++) {
      dp[i][j] = dp[j][i];
      maxmod[a[i] % 7] = max(maxmod[a[i] % 7], dp[i][j]);
      maxnum[a[i]] = max(maxnum[a[i]], dp[i][j]);
      ans = max(ans, dp[i][j]);
    }
    for (int i = j + 1; i <= n; i++) {
      dp[i][j] = max(dp[j][0], max(maxmod[a[i] % 7],
                                   max(maxnum[a[i] + 1], maxnum[a[i] - 1]))) +
                 1;
      maxmod[a[i] % 7] = max(maxmod[a[i] % 7], dp[i][j]);
      maxnum[a[i]] = max(maxnum[a[i]], dp[i][j]);
      ans = max(dp[i][j], ans);
    }
  }
  printf("%d\n", ans);
  return 0;
}
