#include <bits/stdc++.h>
using namespace std;
int n, a[5005];
short nxt[5005][5005][3];
int dp[5005][5005], ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int x = 1; x <= n; ++x) {
    nxt[0][x][0] = nxt[0][x][1] = nxt[0][x][2] = x;
    nxt[x][n + 1][0] = nxt[x][n + 1][1] = nxt[x][n + 1][2] = n + 1;
    for (int y = n; y > x; --y) {
      nxt[x][y][0] = nxt[x][y + 1][0];
      nxt[x][y][1] = nxt[x][y + 1][1];
      nxt[x][y][2] = nxt[x][y + 1][2];
      if (a[x] - a[y] == 1) nxt[x][y][0] = y;
      if (a[x] - a[y] == -1) nxt[x][y][1] = y;
      if (a[x] % 7 == a[y] % 7) nxt[x][y][2] = y;
    }
  }
  ans = 0;
  for (int y = 1; y <= n; ++y)
    for (int x = 0; x < y; ++x) {
      dp[x][y] = max(dp[x][y], dp[0][x] + 1);
      ans = max(ans, dp[x][y]);
      for (int i = 0; i < 3; ++i) {
        int j;
        j = nxt[x][y + 1][i];
        if (j <= n) dp[y][j] = max(dp[y][j], dp[x][y] + 1);
        j = nxt[y][y + 1][i];
        if (j <= n) dp[x][j] = max(dp[x][j], dp[x][y] + 1);
      }
    }
  printf("%d\n", ans);
  return 0;
}
