#include <bits/stdc++.h>
using namespace std;
int a[5005];
int f[100005], g[100005];
int p[5005][5005];
int n;
int ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= n; j++) f[a[j]] = 0, g[a[j] % 7] = 0;
    for (int j = 1; j <= i - 1; j++) {
      f[a[j]] = max(f[a[j]], p[j][i]);
      g[a[j] % 7] = max(g[a[j] % 7], p[j][i]);
    }
    for (int j = i + 1; j <= n; j++) {
      p[i][j] = max(f[a[j] - 1], f[a[j] + 1]) + 1;
      p[i][j] = max(p[i][j], g[a[j] % 7] + 1);
      p[i][j] = max(p[i][j], p[i][0] + 1);
      p[j][i] = p[i][j];
      f[a[j]] = max(f[a[j]], p[j][i]);
      g[a[j] % 7] = max(g[a[j] % 7], p[j][i]);
      ans = max(ans, p[i][j]);
    }
  }
  cout << ans;
  return 0;
}
