#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005, maxv = 1e5 + 5;
int n, a[maxn], dp[maxn][maxn], maxnum[maxv], maxmod[7];
bool vis[maxn][maxn];
int go(int x, int y) {
  if (x > n || y > n) return (x && y ? 0 : -n);
  int &ret = dp[x][y];
  bool &done = vis[x][y];
  if (done) return ret;
  done = 1;
  ret = 0;
  for (int i = max(x, y) + 1; i <= n; i++) {
    if (!x || abs(a[x] - a[i]) == 1 || a[x] % 7 == a[i] % 7)
      ret = max(ret, 1 + go(i, y));
    if (!y || abs(a[y] - a[i]) == 1 || a[y] % 7 == a[i] % 7)
      ret = max(ret, 1 + go(x, i));
  }
  return ret;
}
int get(int i, int j) {
  if (i < j) swap(i, j);
  return dp[i][j];
}
int go_iter() {
  int ans = 0;
  for (int y = 0; y < n; y++) {
    memset(maxnum, 0, sizeof maxnum);
    memset(maxmod, 0, sizeof maxmod);
    for (int x = 0; x <= n; x++)
      if (x != y) {
        int &ret = (x > y ? dp[x][y] : dp[y][x]);
        if (x > y) {
          ret = max(1 + maxnum[a[x] - 1], 1 + maxnum[a[x] + 1]);
          if (y) ret = max(ret, 1 + dp[y][0]);
          ret = max(ret, 1 + maxmod[a[x] % 7]);
        }
        if (x > 0) {
          maxmod[a[x] % 7] = max(maxmod[a[x] % 7], ret);
          maxnum[a[x]] = max(maxnum[a[x]], ret);
        }
        ans = max(ans, ret);
      }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cout << go_iter() << endl;
  return 0;
}
