#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int ans = 0;
  vector<vector<int> > dp(n + 1, vector<int>(n + 1));
  vector<int> maxmod(7, 0), maxnum(100001, 0);
  for (int j = 0; j <= n; j++) {
    for (int i = 0; i <= n; i++) {
      if (i == j) {
        dp[i][j] = 0;
        continue;
      }
      if (j > i) {
        dp[i][j] = dp[j][i];
        if (i != 0) {
          maxmod[a[i - 1] % 7] = max(maxmod[a[i - 1] % 7], dp[i][j]);
          maxnum[a[i - 1]] = max(maxnum[a[i - 1]], dp[i][j]);
        }
        continue;
      }
      dp[i][j] = max(dp[0][j] + 1, maxmod[a[i - 1] % 7] + 1);
      if (a[i - 1] != 100000)
        dp[i][j] = max(dp[i][j], maxnum[a[i - 1] + 1] + 1);
      if (a[i - 1] != 0) dp[i][j] = max(dp[i][j], maxnum[a[i - 1] - 1] + 1);
      ans = max(ans, dp[i][j]);
      maxmod[a[i - 1] % 7] = max(maxmod[a[i - 1] % 7], dp[i][j]);
      maxnum[a[i - 1]] = max(maxnum[a[i - 1]], dp[i][j]);
    }
    for (int i = 0; i < n; i++) {
      maxmod[a[i] % 7] = 0;
      maxnum[a[i]] = 0;
    }
  }
  cout << ans;
}
