#include <bits/stdc++.h>
using LL = long long;
const int N = 3e5 + 10;
int xx[N];
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  std::cin >> n >> k;
  int *dp = xx + 5;
  int r = 0;
  for (int i = 0; i < n; ++i) {
    int now = 0, x;
    std::cin >> x;
    for (int j = -3; j <= 3; ++j) {
      now = std::max(now, dp[x + j]);
    }
    dp[x] = std::max(dp[x], 1 + now);
    r = std::max(r, dp[x]);
  }
  std::cout << (r) << std::endl;
  return 0;
}
