#include <bits/stdc++.h>
using namespace std;
void update_max(short &x, int value) { x = max<short>(x, value); }
short dp[5000][5000] = {0};
short best[5000][5000] = {0};
short best_fwd[5000][5000] = {0};
short best_mod[5000][7] = {0};
short best_mod_fwd[5000][7] = {0};
int main() {
  int n;
  cin >> n;
  set<int> st;
  vector<int> data(n);
  for (long long _n = n, i = 0; i < _n; ++i) {
    cin >> data[i];
    st.insert(data[i]);
  }
  int s = st.size();
  vector<short> data_to_position(100002, -1);
  int index = 0;
  for (auto it : st) {
    data_to_position[it] = index;
    ++index;
  }
  vector<short> single(n);
  short result = 0;
  for (long long _n = n, i = 0; i < _n; ++i) {
    single[i] = 1;
    for (long long _n = i, j = 0; j < _n; ++j) {
      if ((data[i] - data[j]) % 7 == 0 || abs(data[i] - data[j]) == 1) {
        update_max(single[i], single[j] + 1);
      }
    }
    for (long long _n = i, j = 0; j < _n; ++j) {
      dp[i][j] = single[j] + 1;
      short pos_i = data_to_position[data[i]];
      short pos_i_plus = data_to_position[data[i] + 1];
      short pos_i_minus = data_to_position[data[i] - 1];
      short pos_j = data_to_position[data[j]];
      short pos_j_plus = data_to_position[data[j] + 1];
      short pos_j_minus = data_to_position[data[j] - 1];
      update_max(dp[i][j], 1 + best_mod[j][data[i] % 7]);
      if (pos_i_plus != -1) update_max(dp[i][j], 1 + best[j][pos_i_plus]);
      if (pos_i_minus != -1) update_max(dp[i][j], 1 + best[j][pos_i_minus]);
      update_max(dp[i][j], 1 + best_mod_fwd[j][data[i] % 7]);
      if (pos_i_plus != -1) update_max(dp[i][j], 1 + best_fwd[j][pos_i_plus]);
      if (pos_i_minus != -1) update_max(dp[i][j], 1 + best_fwd[j][pos_i_minus]);
      update_max(best_mod[i][data[j] % 7], dp[i][j]);
      update_max(best[i][pos_j], dp[i][j]);
      update_max(best_mod_fwd[j][data[i] % 7], dp[i][j]);
      update_max(best_fwd[j][pos_i], dp[i][j]);
      update_max(result, dp[i][j]);
    }
  }
  cout << result;
}
