#include <bits/stdc++.h>
using namespace std;
int n, rst = 0, a[5007], len[5007], dp[5007][5007];
int last[100007], last_mod[8], next_mod[8][5007];
vector<int> occ[100007], occ_mod[8];
void single_sequence() {
  for (int i = 1; i <= (n); ++i) {
    len[i] = 1;
    len[i] = max(len[i], len[last[a[i] - 1]] + 1);
    len[i] = max(len[i], len[last[a[i] + 1]] + 1);
    len[i] = max(len[i], len[last_mod[a[i] % 7]] + 1);
    last[a[i]] = i;
    last_mod[a[i] % 7] = i;
  }
}
void go_mod(int i, int j, int x, bool which) {
  int to_mod = next_mod[x][j];
  if (to_mod <= j) return;
  if (which)
    dp[i][to_mod] = max(dp[i][to_mod], dp[i][j] + 1);
  else
    dp[to_mod][j] = max(dp[to_mod][j], dp[i][j] + 1);
}
void go(int i, int j, int x, bool which) {
  if (occ[x].empty() || occ[x].back() <= j) return;
  int to = *upper_bound((occ[x]).begin(), (occ[x]).end(), j);
  if (which)
    dp[i][to] = max(dp[i][to], dp[i][j] + 1);
  else
    dp[to][j] = max(dp[to][j], dp[i][j] + 1);
}
void solve_dp() {
  for (int i = 1; i <= (n); ++i)
    for (int j = 1; j <= (n); ++j) {
      if (i > j)
        dp[i][j] = len[j] + 1;
      else if (j > i)
        dp[i][j] = len[i] + 1;
    }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      dp[i][j] = dp[j][i] = max(dp[i][j], dp[j][i]);
      go(i, j, a[i] + 1, 0);
      go(i, j, a[i] - 1, 0);
      go(i, j, a[j] + 1, 1);
      go(i, j, a[j] - 1, 1);
      go_mod(i, j, a[i] % 7, 0);
      go_mod(i, j, a[j] % 7, 1);
      rst = max(rst, dp[i][j]);
    }
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i <= (n); ++i) {
    cin >> a[i];
    occ[a[i]].push_back(i);
    occ_mod[a[i] % 7].push_back(i);
  }
  for (int i = 1; i <= (n); ++i) {
    for (int mod = 0; mod < (7); ++mod) {
      if (!occ_mod[mod].empty() && occ_mod[mod].back() > i)
        next_mod[mod][i] =
            *upper_bound((occ_mod[mod]).begin(), (occ_mod[mod]).end(), i);
    }
  }
  single_sequence();
  solve_dp();
  cout << rst;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  solve();
  return 0;
}
