#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
int n;
int a[maxn];
int dp[maxn][maxn];
int maxmod[500005], maxnum[500005];
int calc() {
  int res = 0;
  for (int y = 0; y <= n; y++) {
    memset(maxmod, 0, sizeof(maxmod));
    memset(maxnum, 0, sizeof(maxnum));
    for (int x = 0; x < y; x++) {
      dp[x][y] = dp[y][x];
      maxmod[a[x] % 7] = max(maxmod[a[x] % 7], dp[x][y]);
      maxnum[a[x]] = max(maxnum[a[x]], dp[x][y]);
    }
    dp[y][y] = 0;
    for (int x = y + 1; x <= n; x++) {
      dp[x][y] = max(dp[x][y], maxmod[a[x] % 7] + 1);
      dp[x][y] = max(dp[x][y], maxnum[a[x] - 1] + 1);
      dp[x][y] = max(dp[x][y], maxnum[a[x] + 1] + 1);
      dp[x][y] = max(dp[x][y], dp[0][y] + 1);
      maxmod[a[x] % 7] = max(maxmod[a[x] % 7], dp[x][y]);
      maxnum[a[x]] = max(maxnum[a[x]], dp[x][y]);
      res = max(res, dp[x][y]);
    }
  }
  return res;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cout << calc() << endl;
  return 0;
}
