#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  vector<int> dp(n, 1e9);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (lower_bound(dp.begin(), dp.end(), a[i] - 2) !=
        lower_bound(dp.begin(), dp.end(), a[i] + 2)) {
      *lower_bound(dp.begin(), dp.end(), a[i] - 2) = a[i];
      *lower_bound(dp.begin(), dp.end(), a[i] + 2) = a[i];
    } else {
      *lower_bound(dp.begin(), dp.end(), a[i] - 2) = a[i];
    }
  }
  for (int i = 0; i < n; ++i) {
    if (dp[i] != 1e9) ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
