#include <bits/stdc++.h>
using namespace std;
int num[5005];
int dp[5005][5005];
int maxnum[100005];
int maxmod[10];
int main() {
  int n;
  while (cin >> n) {
    int ans = 0;
    memset(num, 0, sizeof(num));
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= n; i++) {
      cin >> num[i];
    }
    for (int i = 0; i <= n; i++) {
      memset(maxnum, 0, sizeof(maxnum));
      memset(maxmod, 0, sizeof(maxmod));
      for (int j = 0; j <= i; j++) {
        maxmod[num[j] % 7] = max(maxmod[num[j] % 7], dp[i][j]);
        maxnum[num[j]] = max(maxnum[num[j]], dp[i][j]);
      }
      for (int j = i + 1; j <= n; j++) {
        dp[i][j] = max(maxnum[num[j] + 1] + 1, dp[i][j]);
        dp[i][j] = max(maxnum[num[j] - 1] + 1, dp[i][j]);
        dp[i][j] = max(maxmod[num[j] % 7] + 1, dp[i][j]);
        dp[i][j] = max(dp[i][0] + 1, dp[i][j]);
        maxnum[num[j]] = max(maxnum[num[j]], dp[i][j]);
        maxmod[num[j] % 7] = max(maxmod[num[j] % 7], dp[i][j]);
        dp[j][i] = dp[i][j];
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
}
