#include <bits/stdc++.h>
using namespace std;
int a[5005], n, dp[5005][5005], modval[10], valmx[100005];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int i, j, k;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (i = 0; i <= n; i++) {
    memset(modval, 0, sizeof modval);
    memset(valmx, 0, sizeof valmx);
    for (j = 0; j <= n; j++) {
      if (j > i) {
        dp[i][j] = max(dp[i][j], 1 + modval[a[j] % 7]);
        dp[i][j] = max(dp[i][j], 1 + valmx[a[j] - 1]);
        dp[i][j] = max(dp[i][j], 1 + valmx[a[j] + 1]);
        dp[i][j] = max(dp[i][j], 1 + dp[i][0]);
        dp[j][i] = dp[i][j];
        ans = max(ans, dp[i][j]);
      }
      modval[a[j] % 7] = max(modval[a[j] % 7], dp[i][j]);
      valmx[a[j]] = max(valmx[a[j]], dp[i][j]);
    }
  }
  cout << ans << "\n";
  return 0;
}
