#include <bits/stdc++.h>
using namespace std;
vector<int> A[7], B[100005];
int n, a[5005], f[5005][5005], ans;
int main() {
  int i, j, u, v, w, p, q, r;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = n; i >= 1; i--) {
    A[a[i] % 7].push_back(i);
    B[a[i]].push_back(i);
  }
  ans = 2;
  for (i = 1; i <= n; i++) {
    A[a[i] % 7].pop_back();
    B[a[i]].pop_back();
    u = A[a[i] % 7].size() ? A[a[i] % 7][A[a[i] % 7].size() - 1] : 0;
    v = B[a[i] - 1].size() ? B[a[i] - 1][B[a[i] - 1].size() - 1] : 0;
    w = B[a[i] + 1].size() ? B[a[i] + 1][B[a[i] + 1].size() - 1] : 0;
    for (j = 0; j < i; j++) {
      f[i][j] = max(f[i][j], f[j][0] + 1);
      ans = max(ans, f[i][j]);
      if (u) f[u][j] = max(f[u][j], f[i][j] + 1);
      if (v) f[v][j] = max(f[v][j], f[i][j] + 1);
      if (w) f[w][j] = max(f[w][j], f[i][j] + 1);
      if (!j) continue;
      p = A[a[j] % 7].size() ? A[a[j] % 7][A[a[j] % 7].size() - 1] : 0;
      q = B[a[j] - 1].size() ? B[a[j] - 1][B[a[j] - 1].size() - 1] : 0;
      r = B[a[j] + 1].size() ? B[a[j] + 1][B[a[j] + 1].size() - 1] : 0;
      if (p) f[p][i] = max(f[p][i], f[i][j] + 1);
      if (q) f[q][i] = max(f[q][i], f[i][j] + 1);
      if (r) f[r][i] = max(f[r][i], f[i][j] + 1);
    }
  }
  cout << ans;
  return 0;
}
