#include <bits/stdc++.h>
using namespace std;
int a, b[30005];
map<int, int> dp[30005];
map<int, int>::iterator tmp;
int dd[30005][405];
inline void go(int i, int d, int s) {
  if ((i += d) > 30000) return;
  dp[i][d] = max(s, dp[i][d]);
}
inline void gg(int i, int d, int s) {
  if ((i += d) > 30000) return;
  dd[i][d] = max(s, dd[i][d]);
}
int main() {
  int n, d, s = 0, s0, i;
  for (scanf("%d%d", &n, &d), i = 0; i < n; i++) scanf("%d", &a), b[a]++;
  if (d > 300) {
    dp[d][d] = 0;
    for (i = 0; i <= 30000; i++) {
      for (tmp = dp[i].begin(); tmp != dp[i].end(); tmp++) {
        d = tmp->first, s0 = tmp->second;
        s = max(s, s0 = dp[i][d] + b[i]);
        go(i, d - 1, s0);
        go(i, d, s0);
        go(i, d + 1, s0);
      }
    }
  } else {
    dd[d][d] = 1;
    for (i = 0; i <= 30000; i++) {
      for (d = 1; d <= 400; d++) {
        if (dd[i][d]) {
          s = max(s, s0 = dd[i][d] + b[i]);
          gg(i, d - 1, s0);
          gg(i, d, s0);
          gg(i, d + 1, s0);
        }
      }
    }
    s--;
  }
  printf("%d\n", s);
  return 0;
}
