#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 5;
const int mod = 7;
int dp[maxn][maxn], a[maxn], luu[maxn], n, num[100007];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int ans(0);
  for (int i = 0; i <= n; i++) {
    memset(luu, 0, sizeof(luu));
    memset(num, 0, sizeof(num));
    for (int j = 0; j < i; j++) {
      dp[j][i] = dp[i][j];
      num[a[j]] = max(num[a[j]], dp[j][i]);
      luu[a[j] % 7] = max(luu[a[j] % 7], dp[j][i]);
    }
    for (int j = i + 1; j <= n; j++) {
      dp[j][i] =
          max(max(num[a[j] - 1], num[a[j] + 1]), max(dp[0][i], luu[a[j] % 7])) +
          1;
      num[a[j]] = max(num[a[j]], dp[j][i]);
      luu[a[j] % 7] = max(luu[a[j] % 7], dp[j][i]);
      ans = max(ans, dp[j][i]);
    }
  }
  cout << ans;
  return 0;
}
