#include <bits/stdc++.h>
using namespace std;
const int N = 5005, NUM = 100005;
int a[N], f[N][N], seven[7], num[NUM];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int ans = 0;
  for (int j = 0; j <= n; ++j) {
    memset(seven, 0, sizeof(seven));
    for (int i = 1; i <= n; ++i) num[a[i]] = 0;
    for (int i = 0; i <= n; ++i) {
      if (i == j) {
        f[i][j] = 0;
        continue;
      }
      if (i < j) {
        f[i][j] = f[j][i];
      } else {
        f[i][j] = max(num[a[i] - 1], num[a[i] + 1]) + 1;
        f[i][j] = max(seven[a[i] % 7] + 1, f[i][j]);
        f[i][j] = max(f[0][j] + 1, f[i][j]);
      }
      ans = max(ans, f[i][j]);
      if (i == 0) continue;
      num[a[i]] = max(num[a[i]], f[i][j]);
      seven[a[i] % 7] = max(seven[a[i] % 7], f[i][j]);
    }
  }
  cout << ans;
  return 0;
}
