#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
using pis = pair<int, string>;
struct SegmentTree {
  vector<ll> d;
  int size = 1;
  SegmentTree() : d(1 << 21, 0) {}
  void init_SegmemtTree(vector<ll>& original) {
    int n = original.size();
    while (size < n) size *= 2;
    if (size > (1 << 20)) cout << "Too Many Elements" << endl;
    for (int i = 0; i < n; i++) d[size + i] = original[i];
    for (int i = size - 1; i > 0; i--) d[i] = d[2 * i] + d[2 * i + 1];
  }
  void init_SegmentTree_size(int n) {
    while (size < n) size *= 2;
    return;
  }
  void update(int k, ll a) {
    k += size;
    ll temp = 0;
    while (k > 0) {
      d[k] = max(d[k], a);
      k /= 2;
    }
  }
  ll query(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r == -1) r = size;
    if ((r <= a) || (b <= l)) return 0;
    if ((a <= l) && (r <= b))
      return d[k];
    else
      return max(query(a, b, 2 * k, l, (l + r) / 2),
                 query(a, b, 2 * k + 1, (l + r) / 2, r));
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  SegmentTree st;
  st.init_SegmentTree_size(300001);
  for (int i = 0; i < n; i++) {
    st.update(a[i], st.query(a[i] - 3, a[i] + 4) + 1);
  }
  ll ans = 0;
  for (int i = 0; i < 200001; i++) {
    ans = max(ans, st.d[i + st.size]);
  }
  cout << ans << endl;
}
